2
I need to make a code where the user fills in an array as many times as he wants, but the code stops running. If I register only 3 values, like (1) (2) (3) it works normally, but in some moments (there is no default) the program stops running. I couldn’t find my fault.
Follows the code:
#include<stdio.h>
main(){
int *vetor, i, d, contador=0;
char c;
vetor=NULL;
do{
if(contador==0)
vetor=malloc(1*sizeof(int));
else
realloc(vetor,1*sizeof(int));
printf("Digite um valor para salvar no vetor: ");
scanf("%d", &d);
*(vetor+contador)=d;
contador++;
printf("Deseja cadastrar mais um numero? ");
scanf(" %c", &c);
system("pause");
} while(c == 's' || c == 'S');
system("cls");
for(i=0; i<contador; i++)
printf(" (%d) ", vetor[i]);
}
your
realloc
is relocating the same space tryrealloc(vetor,tamanho*sizeof(int));
where for each loop thetamanha
increments +1– Brumazzi DB