1

I've been trying to solve this problem for hours now. Although I found some similar questions, it just won't work. I have a union within a struct. Now I want to initialize a const variable of this struct.

struct length
{
   int minutes;
   int seconds;
};

typedef struct article
{
   char name[MAXLENGTH_A]
   double price;
   char type;
   union size
   {
      int pages;
      struct length blength;
   } bsize
} art;

Now I want to initialize a const variable of this struct. I read somewhere that the following should work, but it doesn't. I always get the errors: C2224: The operand to the left of '.pages' is not a class, structure, or union and C2078: To many Initializers

const art book = {"Title", 24.99, NORMAL, { .pages = 50}};

I know that this example could be solved easier. But my real problem is, to initialize the 2nd element of the union, like this:

const art book = {"Title", 24.99, AUDIO, { .blength.seconds = 40}};

Neither the first, nor the second initialization is working. Can someone tell me how do it right? I'm using C99 btw.

The Floe
  • 516
  • 5
  • 14
  • 2
    error code format `C2224` suggests you might be using the Microsoft compiler, which doesn't actually support C99. In C90 it is impossible to initialize any member of a union other than the first. You'll have to use a modern compiler or change your stored data structure. – M.M Mar 15 '15 at 21:39
  • 1
    Hm, youre right. I'm using Visual studios. Than I was mistaken, sorry. Would the last example work, if I compile with GCC? – The Floe Mar 15 '15 at 22:00

2 Answers2

1

The { .pages = 50} construct is a designated initializer, a C99 feature unsupported by the MS C compiler (which is a C89 compiler, I'm told). This also restricts your ability to initialize unions only via their first member.

I can see these ways around this limitation: use { 50 } to initialize pages. Then forget the const and explicitly initialize .blength.seconds. The effects of const declaring objects are, uhm, overrated :-)

Jens
  • 69,818
  • 15
  • 125
  • 179
  • It compiles with VS2015 CTP6. See also https://stackoverflow.com/questions/27826409/what-is-the-official-status-of-c99-support-in-vs2013, but VS2013 had and apparently still has some issues with this _new_ feature. – cremno Mar 15 '15 at 22:04
  • @MattMcNabb Thanks for pointing that out, I have changed my answer accordingly. – Jens Mar 15 '15 at 22:08
  • You're right, thanks. I was sure, that Visual Studios is supporting C99. Would my Code work with GCC? I need to Compile it with GCC anyway at the end. The problem is, that I need the const, because our prof wants us to use it. (No I can't ask him, it's complicated). – The Floe Mar 15 '15 at 22:15
  • In embedded, the const is highly rated as you can save valuable ram area by storing the stuff in Flash. Probably not a concern somewhere that MSVC is used though :) In fact OP might be better off having the format in the source file be different to the format stored in memory for searching, that would make this issue moot. – M.M Mar 15 '15 at 22:19
  • I have to use C, so OP is kinda difficult ^^. But a friend managed to compile it with GCC and it worked. I just missed that VS doesn't support C99 and that was the problem. So, thank you bouth for your super fast answers. – The Floe Mar 15 '15 at 22:35
-1

You need to initialise minutes and seconds You don't need to explicitly specify member names {"title",24.99,NORMAL,50,40} Should sort your problem

Key is the members pages and length.minutes occupy the same memory space as its inside a union

Hope it helps

George
  • 1,330
  • 1
  • 9
  • 12