Q:

fscanf not saving line - C

I'm trying to scan a line from a open file pointer, the one passed to the function is stdin. When I print out the value of input I get (null). Any ideas why fscanf is not saving the value? This is the code — also shown below:
char *ReadLineFile(FILE *infile){
    char *input;
    char buf[MAX];
    //check if file pointer is at end of file
    if(feof(infile))
        return NULL;
    //scan from file
    fscanf(infile,"%s",input);
    printf("%s",input);
    input = (char *)malloc(strlen(input)+1);
    //handle memory allocation errors
    if (input == NULL){
        printf("Error allocating memory\n");
        return "error";
    }
    fclose(infile);
    return input;
}

A:

You must allocate memory for input before using fscanf(infile,"%s",input);.
fscanf(infile,"%s",input); asks fscanf to read a string and write it to input. Since input has not been assigned a value, let alone a value that points to memory allocated for this, the behavior is not defined.

