When I try to pass a malloc'd char pointer from main to use in user_input to get a string in it. After the while loop is done I'm trying to print filename. filename ends up to be empty when it should contain a valid string.
void user_input(char *filename){
char ch;
int i = 0;
printf("Fil att leta i: ");
while((ch = getchar()) == '\n'){
filename = realloc(filename, (i+1) * sizeof(char));
filename[i] = ch;
i++;
}
filename[i] = '\0';
printf("filnamn = %s", filename);
}
int main(void){
char *filename;
filename = (char *)malloc(sizeof(char));
user_input(filename);
return 0;
}
sizeof(char)is defined as 1. There's absolutely no reason to ask whether acharis more than onecharbig.