0

I am getting an error when trying to compile a C source file that uses a structure. I'm using gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 on Ubuntu 12.04 LTS.

Here is the code:

/* struct.c: Illustrates structures */
#include <stdio.h>
#include <string.h>

struct Hitter {
    char last[16];
    char first[11];
    int home_runs;
};

int main() {
    struct Hitter h1 = {"McGwire", "Mark", 70};
    struct Hitter h2;
    strcpy(h2.last, "Sosa");
    strcpy(h2.first, "Sammy");
    h2.home_runs = h1.home_runs - 4;
    printf("#1 == {%s, %s, %d}\n",
           h1.last, h1.first, h1.home_runs);
    printf("#2 == {%s, %s, %d}\n",
           h2.last, h2.first, h2.home_runs);
    return 0;
}

and here is the error:

$ gcc -o struct struct.c
struct.c: In function `main':
struct.c:12:9: error: parameter `h1' is initialized
struct.c:14:2: error: expected declaration specifiers before `strcpy'
struct.c:15:2: error: expected declaration specifiers before `strcpy'
struct.c:16:2: error: expected declaration specifiers before `h2'
struct.c:18:2: error: expected declaration specifiers before `printf'
struct.c:21:2: error: expected declaration specifiers before `printf'
struct.c:23:2: error: expected declaration specifiers before `return'
struct.c:24:1: error: expected declaration specifiers before `}' token
struct.c:13:16: error: declaration for parameter `h2' but no such parameter
struct.c:12:16: error: declaration for parameter `h1' but no such parameter
struct.c:24:1: error: expected `{' at end of input

The above code is from the CD training course "Thinking in C" by Chuck Allison. I know that is a very old CD and I am sure the syntax of the structure must have changed, but I have no idea what it may be now. Your help is appreciated. Thanks

10
  • 2
    At first glance this code seems OK to me. Commented Sep 28, 2012 at 15:31
  • 1
    @Mat seems that the version number of GCC is negative... Commented Sep 28, 2012 at 15:33
  • Strange, I'm using gcc 4.5.2 and it compiles fine. Commented Sep 28, 2012 at 15:37
  • Can you upload the output of gcc -E struct.c to a site like gist.github.com? Commented Sep 28, 2012 at 15:37
  • 2
    Looks like the code you've posted isn't what you have in your file, or you have funky chars in your file. The error message states h1 is a parameter, which it isn't. And the {} are perfectly balanced. Either that or one of the standard headers got screwed up, but that's unlikely. Commented Sep 28, 2012 at 15:39

1 Answer 1

1

Once I've had a similar case and it turned out the file I was trying to compile had incorrect line endings. For example, your file taken from disk might have CR + LF as end of line, while only LF is expected. This can be discovered by turning an option to show invisible characters in most of text editors.

Sign up to request clarification or add additional context in comments.

1 Comment

This helped me change my editor settings when working in Windows and compiling in Linux. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.