7

If a struct is only used in one function, can I declare it in that function? Can I do this:

int func()
{
    struct {
        int a, b;
    } s;

    s.a=5;

    return s.a;
}

gcc choked on it, but it emitted a very weird looking error that I couldn't understand instead of saying "Sorry, you can't do that".

Baruch
  • 20,590
  • 28
  • 126
  • 201

1 Answers1

10

This is perfectly valid C89/C99/C11 code, this a structure with no tag and the object has block scope. Check C99 6.7.2.3p6 to see the identifier for the tag is optional.

ouah
  • 142,963
  • 15
  • 272
  • 331