21

If it's a struct then it can be done

*p = {var1, var2..};

But seems this doesn't work with union:

union Ptrlist
{
        Ptrlist *next;
            State *s;
};

Ptrlist *l;
l = allocate_space();
*l = {NULL};

Only to get:

expected expression before ‘{’ token
einpoklum
  • 118,144
  • 57
  • 340
  • 684
lexer
  • 1,027
  • 3
  • 14
  • 18
  • 1
    you have two pointer types in your union. which one are you trying to initialize to null? how do you expect the compiler (or this reader) to know? – James Wilcox Aug 10 '11 at 09:49
  • @James Wilcox ,I know I can do it by `u.field1=NULL;u.field2=NULL` but isn't that kind of redundancy? – lexer Aug 10 '11 at 09:53
  • the question is which field you're trying to initialize in the above code. neither me nor the compiler has any idea if it's supposed to be `next` or `s` that you're trying to access. – James Wilcox Aug 10 '11 at 09:54
  • @James Wilcox,I want to initialize all fields to NULL. – lexer Aug 10 '11 at 09:56
  • 1
    I think you misunderstand what a `union` is then. only one field may be (safely) used at a time. if you want to be able to access both fields, you need a `struct`. if you just want to make sure the whole damn thing is nulled out, you can pick either one in this case, since the pointer types (probably) have the same length. – James Wilcox Aug 10 '11 at 09:58
  • `*p = {var1, var2..};` does not work for `struct` either – M.M Nov 30 '14 at 20:46
  • 1
    @JamesWilcox - ptr types probably have the same length, and not less important will most probably occupy the same space in memory (i.e., alignment). But, this is not guarantied, so I'd consider your comment as "dangerous". One could use a declaration attribute like "packed" or similar to increase the chances that this is correct, but once you go to the realm of implementation dependent code, then you could verify that the members are aligned even w/o the attribute. – ysap Feb 08 '18 at 18:48

5 Answers5

35

In C99, you can use a designated union initializer:

union {
      char birthday[9];
      int age;
      float weight;
      } people = { .age = 14 };

In C++, unions can have constructors.

In C89, you have to do it explicitly.

typedef union {
  int x;
  float y;
  void *z;
} thing_t;

thing_t foo;
foo.x = 2;

By the way, are you aware that in C unions, all the members share the same memory space?

int main () 
{
   thing_t foo;
   printf("x: %p  y: %p  z: %p\n",
     &foo.x, &foo.y, &foo.z );
   return 0;
}

output:

x: 0xbfbefebc y: 0xbfbefebc z: 0xbfbefebc

Community
  • 1
  • 1
Crashworks
  • 40,496
  • 12
  • 101
  • 170
3

There is difference between initialization and assignment. Initialization is intelligent, while for the assignment you need to resolve proper address.


    Example
    char str[] = "xyz";    // works - initialization 
    char str[10];
    str = "xyz";            // error - assignment
                           // str is a address that can hold char not string

    Similarly
    Ptrlist l = {NULL};    // works - initialization 
    Ptrlist *l;
    l->next = NULL;        // works assignment
    l = {NULL};            // is assignment, don't know the address space error

Kamath
  • 4,461
  • 5
  • 33
  • 60
0

Assign one of the fields to NULL. Since it is a union all the fields will be NULL.

duedl0r
  • 9,289
  • 3
  • 30
  • 45
0

I don't have a State class so I replaced it with an int.

this is my code:

union Ptrlist
{
    Ptrlist *next;
    int *n;
};


int main(int argc, char** argv)
{
    Ptrlist *l = new Ptrlist;

    // I'm using a way c++ allocated memory here, you can change it to malloc.
    l->n = new int;
    *(l->n) = 10;

    // Because you used an union, n's and next's addres is same 
    // and this will output 10
    printf("%d", *(l->next));

    getch();
    return 0;
}

So in this way, n's value is initialized to 10

shengy
  • 9,461
  • 4
  • 37
  • 61
0
union Ptrlist1
{
    char *next;
    char  *s;
};

union Ptrlist1 l = {  NULL };

see this a example of union initialization. in your case i think there is some mistake in

  Ptrlist how can be member of union..??

you should write

 union Ptrlist
 {
    union Ptrlist *next;
        State *s;
 };
Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222