Ok so the original code is wrong because I would lose a pointer as Valter says.
array = (int *) realloc (array, alloc * sizeof(*array)); } //wrong
But if I try Valters code the program would crash. See comments below.
int *temp;
temp = (int *) realloc (array, alloc * sizeof(*array)); } //Here ends body of condition
if(temp == NULL){free(array);} //for expanding array
else{ //Lets say we are in firts iteration. temp is not equal NULL
array = temp; //so we ran in the "else" where we try to asign temp to
//continue //array. But theres nothing in temp yet => Program crashes.
}
So I made a little change of code above which works well.
if( i > alloc - 1 ) {
alloc *= 2;
temp = (int *) realloc (array, alloc * sizeof(*array));
if( temp == NULL ) {
free(array);
printf("Error allocating memory.\n");
return 1; }
array = temp; //Now we have confidence that temp would not be empty or what so ever
} //Here ends body of condition for expanding array