0
#include <stdio.h>
  union test {
    int test1;
    int test2;
    int test3;
  };
  int testC(union test *x, int q, int w, int e) {
    (x->test3)=q;
        x--;
    (x->test2)=w;
        x--;
    (x->test1)=e;
        x--;
    return 0;
  };
  int main() {
    union test hi;
    testC(&hi,5,6,7);
    printf("%i, %i, %i \n", hi.test1, hi.test2, hi.test3);
        int x,*y = 0;
        x = 1;
        x++;
        scanf("%i\n", y);
  }

The above code is supposed to create an Union and then "initialize" all its components to the inputs, but instead it just initializes itself to the first one. I've tried x++ and moving the values around, but nothing seems to work.

hat
  • 781
  • 2
  • 14
  • 25
Devan S.
  • 1
  • 4

1 Answers1

0

A union takes up as much space as its largest member requires. That means it can only hold the value of one of its members at a time. The answer below explains it pretty well.

Difference between a Structure and a Union

hmarceau
  • 28
  • 1
  • 6