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

  • 1
    see related question http://stackoverflow.com/questions/330793/how-to-initialize-a-struct-in-ansi-c – TJD Apr 24 '12 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. –  Apr 24 '12 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` – R Samuel Klatchko Apr 24 '12 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. –  Apr 24 '12 at 19:25
  • @RSamuelKlatchko to answer YOUR question: GCC 4.7.0 prerelease –  Apr 24 '12 at 19:27
  • And thanks Klatchko your input led me to the discovery. –  Apr 24 '12 at 19:28
  • 2
    Sounds like a bug in the prerelease. You should let the gcc team know. – R Samuel Klatchko Apr 24 '12 at 19:33
  • @RSamuelKlatchko I will if you can't get it to compile in debug. Or else its unconfirmed. For all I know that's intended operation. –  Apr 24 '12 at 19:35
  • "Bugs are just features or side effects" –  Apr 24 '12 at 19:36

2 Answers2

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.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Whatever is going on its a curious thing. Maybe that's my next question. "Why doesn't this flag a compile warning?" –  Apr 24 '12 at 19:03
  • 1
    @TristonJ.Taylor You dint tell us what was the cause? Were you using a C++ IDE? – Pavan Manjunath Apr 24 '12 at 19:14
  • @PavanManjunath C++ compatible in a C project, in a C file Using GCC in "debug mode" –  Apr 24 '12 at 19:19
  • Ok that did it! Debug configuration was the culprit! –  Apr 24 '12 at 19:23
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>;
}
  • Thanks for your answer! "There is something strange going on.." DeJa Vu.. lol In other words: My sentiments exactly... –  Apr 24 '12 at 19:15
  • Can you confirm the bug? `gcc (GCC) 4.7.0 20120407 (prerelease)` –  Apr 24 '12 at 19:16
  • 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. –  Apr 24 '12 at 19:23