I am doing my C assignment for colleage. The task is requiring the function uses getchar() to read input from user and save it to a char[] type variable. Here is my code:
void readLine(char str[], int length){
int i = 0;
if(length < 1) return;
while(1){
str[i] = getchar();
if(str[i] == '\n'){
str[i] == '\0';
return;
}
if(i < length - 1) i++;
}
}
printf("String? ");
char inputString[LENGTH];
readLine(inputString, LENGTH);
the terminal will get frozen while the value of getchar() is assigned to str[i]. If I assigned it to an int type variable, no complains comes from terminal. According to the doc of getchar(), it is returning int type and it is possible to assign to a char[]. So I am confused what I did wrong.