With the following piece of code, I get a very wierd result. Why is the last element's value overwriting all previous array elements? i suspect there's a bigger problem than just this immmediate problem.
#include <stdio.h>
main()
{
int i, cases;
char num[1000000];
scanf("%d", &cases);
char* array[cases];
//store inputs in array
for(i=0; i<cases; i++)
{
scanf("%s", &num);
array[i] = #
}
//print out array items and their memory addresses
for(i=0; i<cases; i++)
{
printf("%d %s\n", i, array[i]); //print (array index) (array value)
printf("%d %p\n", i, &array[i]); //print (array index) (array address)
}
}
Inputs:
3 <-- number of lines to follow
0 <-- put in array[0]
1 <-- put in array[1]
2 <-- put in array[2]
Outputs
0 3 <-- why is this being overwritten with the last element?
0 0013BCD0
1 3 <-- why is this being overwritten with the last element?
1 0013BCD4
2 3
2 0013BCD8