答案:我没有正确初始化变量。
我需要记住指针声明在 C 中是如何工作的。
这段代码应该采用以下格式的一些命令行参数:
./foo 1 2 3 4 5 6 7 8 9 10 ...(任何合理数量的参数,只要它们能被 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 行,它编译得很好。只是想我会为后代添加。