5

I'm declaring a struct in the IDE (Netbeans) like this:

struct foo { size_t var = 1; }

And the IDE says its legal. I compile the project (its a static lib..) and the compiler says its legal (does not moan or throw).

Now since I'm just testing the syntax at this point I have to ask someone who might know if this will actually work when I go to declare a struct of this tag in my procedures like this:

struct foo myfoo;

So the question is: Is that a valid initalizer? (size_t var = 1) or is the compiler just stroking my ego here? I haven't found anything on google or in documentation like this so I'm guessing it doesn't work the way I hope it will.

*edit So me and the Good 'ol boys here @SO figured out that it will compile in a debug configuration but not a release configuration. Who says one head is better than a couple thousand? :D

9
  • 1
    see related question stackoverflow.com/questions/330793/… Commented Apr 24, 2012 at 18:57
  • @TJD Now that was both quick and awesome! Thanks. But I still don't get why GCC and the IDE let it slip without groaning. Just kicked the warnings up full blast... And I can still hear the crickets chirping. lol. Commented Apr 24, 2012 at 19:02
  • 1
    What version of gcc are you using? I just tried with 4.4.3 and I get an error expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token Commented Apr 24, 2012 at 19:20
  • Yeah, I just checked it myself in release mode versus debug mode. Origianlly I was in debug mode. It won't compile without screaming in release mode. Commented Apr 24, 2012 at 19:25
  • 2
    Sounds like a bug in the prerelease. You should let the gcc team know. Commented Apr 24, 2012 at 19:33

2 Answers 2

4

I don't think it's allowed. You need to specify the value for the variable, not the struct:

struct foo { size_t var; };

struct foo myfoo = { 1 };

As for the IDE allowing it, the obvious possibility would be that the code is really a little different, such as:

struct foo { static const size_t var = 1; }

...and the IDE is compiling it as C++ rather than C.

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

4 Comments

Whatever is going on its a curious thing. Maybe that's my next question. "Why doesn't this flag a compile warning?"
@TristonJ.Taylor You dint tell us what was the cause? Were you using a C++ IDE?
@PavanManjunath C++ compatible in a C project, in a C file Using GCC in "debug mode"
Ok that did it! Debug configuration was the culprit!
1

The simple answer is: You cannot initialize inside the struct definition, so if the compiler is letting you do that there is something strange going on.

To initialize you have to instantiate that struct in a var (or object if that's what you are going for) and initialize that var.

So, for your struct:

struct foo {
    size_t var = 1;
}


You would initiazlize as:

type function_name(...params...) {
    struct foo myFooVar;
    myFooVar.var = 1;
    ....
    return <type>;
}

3 Comments

Thanks for your answer! "There is something strange going on.." DeJa Vu.. lol In other words: My sentiments exactly...
Can you confirm the bug? gcc (GCC) 4.7.0 20120407 (prerelease)
Found it: debug configuration for a project will let that slide, release mode throws the fire of dragons from middle earth.. or something like that.

Your Answer

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