I'm running a program to read in a string in C. I'm having a weird problem with storing the string. My code is:
void getarray(char *text){
char a, *p;
int b = 1;
p = text;
scanf("%c", &a);
if(a == '\n'){
*p = '\0';
return;
}
while(a != '\n'){
*(p+b-1) = a;
p = realloc(p, sizeof(char)*(b+1));
b++;
scanf("%c", &a);
}
*(p+b) = '\0';
}
So this reads in the characters properly, but when it reaches the end of the string, it does nothing, rather than continuing past the while loop. If I print out a
as the last command in the while loop, when it reaches the end of the string it prints a blank space, but the program doesn't leave the loop.
Any help appreciated, thanks!