1
I’m trying to allocate dynamically, but the error is appearing:
Error in . /name. c; invalid : 0x000001c18834
My code:
// FUNCAO //
char *nome(text[])
{
int n3 = 0;
int n2 = 0;
char *n = NULL;
while((n2 = getchar()) != '\0' && n2 != EOF)
{
n3++;
n = realloc(n, n3*sizeof(char));
if(n == NULL)
{
puts("Erro ao realocar!");
exit(0);
free(n);
}
*(n+(n3-1)) = n2;
}
*(n+n3) = '\0';
return n;
}
// USANDO //
int main(void)
{
char *name = nome("Nome:");
while(*name != '\0')
printf("%c", *name++);
free(name); // aqui ta o problema, sem o free roda normal agora com o free me da esse erro
}
This code doesn’t even compile, there are syntax errors, so it gets complicated until you say something. But you tried to use
free(name)
. There’s no reason to overreact.– Maniero
I can’t edit the text here last time someone edits to me
– LavaiBala
To edit your question click on edit.
– Taisbevalle
test someone with the free and without the free
– LavaiBala