2

I'm reading some c codes and find that some struct contains a union name without a variable name, just as the following example:

typedef union Lock Lock;

union Lock{
    uint32 key;
};

struct Test{
    Lock;
    uint32 name1;
};

What does the Lock inside Test mean? PS. the type uint32 has already been defined before the two declaration.

timrau
  • 22,578
  • 4
  • 51
  • 64
  • 3
    That's not valid C. It won't compile as it stands. – cnicutar Apr 11 '13 at 05:54
  • 1
    This code does not compile because the declarations are missing terminating semicolons, `uint32` is not defined, and `Lock;` is not a valid declaration. You should show a [self-contained, compilable example](http://sscce.org) and specify the compiler and switches you are using. – Eric Postpischil Apr 11 '13 at 12:49
  • 1
    @hyde Yes, there is a typedef union Lock Lock. Sorry that this is the first time I try to posted here. I will take care of the format in the future. Thanks. – user2268908 Apr 11 '13 at 14:39

3 Answers3

0

What you posted isn't valid C. As far as the language is concerned, Lock and union Lock are completely different things and that's that.

If however you were to say union Lock inside the struct it would compile but wouldn't do anything: it would be just a superfluous declaration declaring nothing.


There's some speculation that adding a union Lock or Lock at the beginning of the struct does something. A quick check should prove that false:

printf("%zu\n", offsetof(struct Test, name1)); /* Will print 0. */
cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • now if there was a `union Lock` declaration inside the struct would it still be incomplete type?i'm asking because there is already a definition in site? – Koushik Shetty Apr 11 '13 at 06:01
  • @Koushik It won't do anything. The type cannot be incomplete since there's a full-blown declaration just above. – cnicutar Apr 11 '13 at 06:02
  • yes so the `union Lock` inside struct is incomplete type and different from the one declared and defined outside the struct with the same name? – Koushik Shetty Apr 11 '13 at 06:05
  • These two declarations are coming from a header file. I guess it may be like what cnicutar said that the Lock inside struct Test has nothing to do with the Union Lock. But I am not sure about that.. – user2268908 Apr 11 '13 at 06:06
  • @user2268908 Is it possible `Lock` is actually some expanding macro ? – cnicutar Apr 11 '13 at 06:08
  • @cnicutar There is no macro definition in this header file and this header file does not include any other header files. So seems it is not any expanding macro. Is it a special flag for the compiler? – user2268908 Apr 11 '13 at 06:14
  • Is it a plan9 compiler? Iirc it had this kind of construct as an extension – loreb Apr 11 '13 at 15:35
0

If you can compile your code, I guess you are using an old compiler or a compiler which still supports implicit int. This would mean that the compiler interprets your code as

struct Test{
    int Lock;
    uint32 name1;
}

Please see also Why does/did C allow implicit function and typeless variable declarations?

Community
  • 1
  • 1
Werner Henze
  • 16,404
  • 12
  • 44
  • 69
0

So, with typedef added, gcc produces warning:

main.c:8:9: warning: declaration does not declare anything [enabled by default]

Also, simple sizeof reveals that these have same size:

struct Test{
    Lock;
    uint32 name1;
};

struct Test2{
    uint32 name1;
};

I don't think in C there is any scenario, where this can mean anything useful. Maybe it is a confused attempt at forward declaration.

hyde
  • 60,639
  • 21
  • 115
  • 176