C - dynamic arrays -
i don't quite understand how pointers work c arrays. here's code got:
int arrayone[] = {1, 2, 3}; int arraytwo[] = {4, 5, 6, 7}; int **arraythree = (int **)malloc(2 * sizeof(int)); arraythree[0] = arrayone; arraythree[1] = arraytwo; (int = 0; < 2; i++) { int *array = arraythree[i]; int length = sizeof(array) / sizeof(int); (int j = 0; j < length; j++) printf("arraythree[%d][%d] = %d\n", i, j, array[j]); }
i have expected output following:
arraythree[0][0] = 1 arraythree[0][1] = 2 arraythree[0][2] = 3 arraythree[1][0] = 4 arraythree[1][1] = 5 arraythree[1][2] = 6 arraythree[1][3] = 7
what prints out is:
arraythree[0][0] = 1 arraythree[0][1] = 2 arraythree[1][0] = 4 arraythree[1][1] = 5
why?!
sizeof(array)
size of pointer, happens twice size of int
on platform.
there's no way length of array in c. have remember yourself.
Comments
Post a Comment