ANSWER: I wasn’t properly initializing variables.
I need to remember how pointer declarations work in C.
This chunk of code is supposed to take in some command line arguments in the format of:
./foo 1 2 3 4 5 6 7 8 9 10 … (any reasonable number of arguments so long as they’re divisible by 5
My plan is to have 5 int arrays of variable size to store the arguments for use in logic later. So if I have 10 arguments, I’d have 5 2-int arrays, and so on.
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);
}
Problem is, I get these weird errors
>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
And I have no idea what they’re supposed to be or represent. I’ve looked it up and nothing’s helped so far. I was hoping y’all could.
EDIT: For the record I tried commenting out lines 26-30 and 33-37 and it compiles fine. Just thought I’d add that for posterity.
This:
is the same as this:
You want this:
But more generally, try to avoid declaring multiple variables on the same line.