I would like to know why I can't reach to increase my array size by only one in a while loop. Here is my code :
void pb_memory(void){
printf("ERROR : memory problem !\n");
system("PAUSE");
exit(EXIT_FAILURE);
}
int main(int argc, char *argv[])
{
int length = 0;
/*allocate memory and check for no error*/
int *array = calloc(1, sizeof(int)); /*initialize to 0*/
if(array == NULL)
pb_memory();
/*Check for valid inputs and put into array*/
while((scanf("%d", &num)) != EOF){
array[length]=num;
length++;
array = realloc(array, length*sizeof(int));
if(array == NULL)
pb_memory();
}
.
.
.
.
}
Why is this not working ? It fails and goes directly into the pb_memory() function .I would like every time length increase by one my array does too...
Thanks for your help.
EDIT : Sorry guys I wanted to keep the code simple and focused on my problem that's why I didn't write all my variables. Anyway I'm learning for next time and thanks @Michael and everyone who participate.