This is probably a simple question but I'd like a little clarification.
Do these two code pieces do the same thing with the same result?
Code:
int array[2] = {1,2};
int *temp;
temp = realloc(array, 3 * sizeof array);
if(temp != NULL)
array = temp;
array[2] = 3;
printf("%d", array[2]);
and
Code:
int *array;
int *temp;
array = malloc(2 * sizeof *array);
if(array != NULL){
array[0] = 1;
array[1] = 2;
}
temp = realloc(array, 3 * sizeof *array);
if(temp != NULL)
array = temp;
array[2] = 3;
printf("%d", array[2]);
Both of these pieces should change the size of the array to 3, put the integer 3 into the third element and print it without any access violation errors, right?