I have this simple code that seems to work (I checked with the debugger) but when the function execution ends, the string is not saved in the original variable.
void getString(char *iText);
int main()
{
char *inputText=malloc(sizeof(char));
getString(inputText);
puts(inputText);
free(inputText);
system("pause");
return 0;
}
void getString(char *iText)
{
char c;
int i=0;
while((c=getchar()) != '
')
{
iText = realloc(iText,sizeof(char)*(i+1));
iText[i]=c;
i++;
}
iText = realloc(iText, sizeof(char)*(i+1));
iText[i]='';
}
When this little script ends, I see some
ε■ε■ε■ε■ε■ε■ε■ε■ε■ε■ε■ε■▲??`*
If I write this code in my main function it's working, so I'm guessing it's something to do with the way I'm using the pointer in the function.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…