0

I'm stuck with what seems a beginner's compilation error:

My simple program:

#include <stdlib.h>
#include <stdio.h>
#include "Tiles_Circular_Linked_List.h"

#define MAX_LINE_LENGTH 128;

int main(int argc, char **argv) {
    struct node *head_tail;
    FILE *file;
    /*char filename[] = "/home/student/Desktop/Studies/C/testing_fodder/tiles";*/

    argv++; /*go to second character-array argument*/

    file = fopen(*argv, "r");
    char *curr_line;

    fgets(curr_line, MAX_LINE_LENGTH, file);

    return 0;
}

I try to compile it using this command:

gcc -g -Wall -ansi launch_tiles.c -o tiles_prog

and get these errors:

launch_tiles.c: In function ‘main’:

launch_tiles.c:17:19: error: expected ‘)’ before ‘;’ token

launch_tiles.c:17:19: error: too few arguments to function ‘fgets’ /usr/include/stdio.h:628:14: note: declared here

launch_tiles.c:9:8: warning: variable ‘file’ set but not used [-Wunused-but-set-variable]

launch_tiles.c:8:15: warning: unused variable ‘head_tail’ [-Wunused-variable]

I am interested about the errors, not the warnings.

I can count three arguments that I pass to fgets and don't understand where do I miss parentheses so what's the problem?

Thanks!

Max Segal
  • 1,955
  • 1
  • 24
  • 53
  • You have another serious bug: `char *curr_line; fgets(curr_line,` . A decent compiler would catch this, try turning up your warning level, e.g. try `-Wextra`. This code should trigger red flags with you. C uses pass-by-value. You have a pointer that is uninitialized, and you sent a copy of it to a function ... what do you think `fgets` will do with this copy? You should also be aware that using uninitialized variables usually causes undefined behaviour . – M.M Jul 02 '15 at 10:35
  • Yea, thanks I fixed this with malloc.. – Max Segal Jul 02 '15 at 11:26

1 Answers1

5

change

#define MAX_LINE_LENGTH 128;

to

#define MAX_LINE_LENGTH 128

(without the ;). Easy mistake to make.

The C preprocessor does very simple, textual substitution, so with the wrongly-defined macro, you end up with

fgets(curr_line, 128;, file);

which is obviously a syntax error.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
  • OMG Im gonna sue gcc for most ridiculous head_scratches/error_complexity ratio.. thanks! – Max Segal Jul 01 '15 at 23:52
  • 1
    @MaxSegal: I have some bad news for you, my friend: "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE". :-) – Steve Summit Jul 01 '15 at 23:57
  • 2
    @MaxSegal Don't hesitate to use the´ --save-temps´ option in order to see the pre-processed source code (it's a ´.i´ file), it's always interesting. – Bregalad Jul 02 '15 at 10:00