I'm trying to use realloc in this program to increase an array's size, using pointer to pointer. But it doesn't end up doing the task:
#include <stdio.h>
void push(int** data)
{
*data = (int *)realloc(*data, 5 * sizeof(int));
*data[4]=100;
}
int main() {
int *array = (int *)malloc(2 * sizeof(int));
push(&array);
printf("%d", array[4]);
free(array);
return 0;
}
I don't want to use a global variable as the array and I want the changes to be made to the array directly. What should I do?
"But it doesn't end up doing the task"-- This is not a sufficient description of the problem. In what way does the program not work? Do you maybe get an error message from the compiler? If so, then you should state this in your question and post the error message that you are getting.