i try to get numbers from my user and this is my function,
my function get arr as pointer and set it to a new array and return the counter of the number that i get for printing.
but when i try to print the array i get an ERROR that ther is noting
int GetNumber(int *arr)
{
int n,i=0;
int *temp;
temp = (int*)calloc(1,sizeof(int));
assert(temp);
scanf("%d",&n);
while(n != -1)
{
i++;
temp = (int*) realloc(temp,i*sizeof(int));
assert(temp);
temp[i-1] = n;
scanf("%d",&n);
}
arr = temp;
return i;
}
int **arras an argument and realloc using*arr = realloc(*arr, ...), and thus pass a pointer to an int pointer like so:GetNumber(&the_arr)reallocand assign the return to your original variable. Ifreallocfails, the originaltempis NOT freed andNULLis returned and assigned totempcausing you to lose the reference to the block of memory pointed to bytempcreating a memory leak. Instead use a temporary pointer, e.g.void *t = realloc (temp, i * sizeof *temp); if (t) temp = t;