解答:我沒有正確初始化變量。C編譯器錯誤 - 警告:賦值會使指針中的整數不帶轉換|錯誤:下標值既不是數組也不是指針
我需要記住指針聲明中C.
是如何工作的代碼,這個塊應該採取一些命令行參數,格式爲:
./foo 1 2 3 4 5 6 7 8 9 10 ... (any reasonable number of arguments so long as they're divisible by 5
我的計劃是有5個可變大小的int數組,用於存儲稍後在邏輯中使用的參數。所以如果我有10個參數,我會有5個2-int數組,等等。
int* IP1, IP2, BID, PN, EID;
int i;
if((argc < 2) || ((argc-1) % 5) != 0)
{
/* Some error statements */
}
IP1 = (int*)malloc(argc-1);
IP2 = (int*)malloc(argc-1); //This is line 26
BID = (int*)malloc(argc-1);
PN = (int*)malloc(argc-1);
EID = (int*)malloc(argc-1);
for(i = 0; i < argc-1; i+=5){
IP1[i] = argv[i+1];
IP2[i] = argv[i+2]; //This is line 33
BID[i] = argv[i+3];
PN[i] = argv[i+4];
EID[i] = argv[i+5];
printf("%d\t", i);
}
問題是,我得到這些奇怪的錯誤
>cc foo.c -o foo
foo.c: In function ‘main’:
foo.c:26: warning: assignment makes integer from pointer without a cast
foo.c:27: warning: assignment makes integer from pointer without a cast
foo.c:28: warning: assignment makes integer from pointer without a cast
foo.c:29: warning: assignment makes integer from pointer without a cast
foo.c:33: error: subscripted value is neither array nor pointer
foo.c:34: error: subscripted value is neither array nor pointer
foo.c:35: error: subscripted value is neither array nor pointer
foo.c:36: error: subscripted value is neither array nor pointer
而且我不知道他們應該做的是或代表。我查看了它,迄今爲止沒有任何幫助。我希望你們都可以。
編輯:對於記錄,我試着註釋掉行26-30和33-37,它編譯得很好。只是想我會爲後人補充一點。
正如你已經得到答案,還有一個提示 - 你是不是分配太少的內存?它以字節爲單位,必須乘以sizeof(int)。 – keltar
也許吧。在嘗試解決問題時,我更早地更改了它。 – BigDamnHero