I think the following test code produces an error on the execution of this code line: printf( "%d, ", p[i] ); (there are two printf of this kind, i mean the second one after the realloc, near the end of the source code)
I would like somebody to enlighten me if there is a mistake that i do not see...
What i did with my test code: allocate some memory(malloc) save some int numbers, then increase the size of the allocated memory(realloc) to save some additional int numbers. The first time(malloc) i choose( with scanf) the size of the allocated memory to be 5, then with a new scanf i resize it to 10.
Select the total number of input elements: 5
enter array value:
1
enter array value:
2
enter array value:
3
enter array value:
4
enter array value:
0
results:
1,2,3,4,0,
select new size:10
1,2,3,4,0,11,17,14,15,22, <---- the momment i see this line the code breaks and i get a pop up window which says:
mytestprog.exe. has stopped working
windows can check online for a solution to the problem,
->Check online for a solution and close the program
-> close the program
@TarikNeaj
What the OP is trying to achieve is this:
1 2 3 4 5 6 7 8 9 10 11 12
int old_sz = sz; // You need to save the current sz
printf( "\nselect new size:" );
scanf( " %d", &sz );
p = ( int * )realloc( p, sz * sizeof(int) );
if( p != NULL )
{
for ( i = old_sz; i < sz; i++ ) // Note: old_sz
p[i] = 6 + rand() % 12;
for ( i = 0; i < sz; i++ )
printf( "%d, ", p[i] );
}