I wrote this code that reads from a .txt a string and settles it in a char *, but it gives me error that the variable "string" is not initialized, even if i initialized it inside the fscanf, can u tell me where i wrong? Thanks!
char *string;
FILE *fp = fopen("words.txt", "r");
fscanf(fp, "%s", string);
string.stringis a pointer tochar. You've to make it point to achararray(char buffer[100]; string = buffer;) or dynamically allocate memory(string = malloc(100);and after use,free(string);) before you write to it from thefscanf.fscanf, usefreadwhere you can specify the maximum number of bytes to read. That's a bit more work, but safer.getlineif you can. It allocates the right amount of memory.