i don't understand why this program crash if i'll insert a number more of 5 times... i want to allocate dynamically a vector if the input of the user exceeds 5 times... this is the code:
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int * vettore, n;
unsigned int len=1;
vettore = (int *) malloc (5 * sizeof(int));
printf("Gimme a positive integer (0 to terminate): ");
scanf("%d", &n);
for( len=1; n != 0; len++, vettore++ ){
if (len > 5){
vettore = realloc (vettore, len * sizeof(int));
*vettore = n;
printf("\nGimme a positive integer (0 to terminate): ");
scanf("%d", &n);
}
else{
*vettore = n;
printf("\nGimme a positive integer (0 to terminate): ");
scanf("%d", &n);
}
}
vettore--;
while(len>1){
printf("%d ", *vettore--);
len--;
}
printf("\n\n");
system("PAUSE");
return 0;
}
p.s.= if the user's input is less than 5 times no problem... so i think there is a problem that i can't see with the realloc