我有一個很簡單的C代碼麻煩一個結構,其中我需要存儲的char *
的陣列(基本上,一個字符串數組)在一個結構中。簡單C的結構:故障存儲一個char * []到C.類型不兼容
我收到錯誤:
error: incompatible types when assigning to type ‘char *[41]’ from type ‘char **’
下面是相關的代碼和解釋:
struct HistoryElement
{
int NumberOfCommandGiven;
char * command[MAXLINE/2+1];
};
int main() {
char *args[MAXLINE/2+1];
setup(args); //This gets the command given by user and parses it into args. This works properly.
struct HistoryElement input;
input.command = args; //How to accomplish this without error?
printf("input.command: %s",input.command); //Test
}
的錯誤出現在哪裏,我想設置input.command
到args
行。雖然它們都是char * []
類型,但我得到了不兼容的類型錯誤。我確信這個問題有點簡單,但我不明白我做錯了什麼。
這一行: '字符*命令[MAXLINE/2 + 1];'定義一個POINTERS數組,但不爲每個指向這些指針的內存分配內存。 – user3629249 2015-02-24 01:32:32
@ user3629249:顯然,這個想法是,內存由'setup'函數管理,這意味着適當的指針值從'args'數組中的'setup'返回。這些指針被認爲是有效的,即我們的業務不必擔心它們來自哪裏以及它們指向哪裏。 – AnT 2015-02-24 01:58:42