I'm new to c so sorry if the question might seem basic. I'm trying to read a string with unknown size from the user and store it in a dynamic array. This program is going to make accounts for users but I can't successfully scan the username or the password. Please note that no arrays with fixed size can be used. Any solutions using files can be used and also be really helpful.
void main(){
user *dummyUser = (user *) malloc(sizeof(user));
dummyUser->next=NULL;
char* password=(char* )malloc(sizeof(char));
char* name=(char* )malloc(sizeof(char));
int i=0;
char c;
char ptr;
while((c=getchar())!=' '){
name[i]=c;
ptr=(char *)realloc(name,sizeof(char));
if(ptr==NULL){
printf("realloc failed");
exit( EXIT_FAILURE );
}
i++;
}
i=0;
while((c=getchar())!='\n'){
password[i]=c;
password=(char *)realloc(password,sizeof(char));
i++;
}
}
realloc seem to work for 1 time, but the next times it returns a null pointer and makes the program crash. note: I know I have to check if realloc returns a null pointer, I checked it in my watchlist so I didn't write the related code.Thanks for helping.