1

I am trying to find what will happen if I have 2 variables in enum and I assign it a value. I want a understanding on when i assign value to c_type which one gets used C1 or C2?

I have the following code:

typedef enum {
    C1 = 0,
    C2,
} c_type;

typedef struct A_a {
    c_type store;
} A;

FuncABC(int val)
{
    A a1;
    a1.store = val; /here store has C1 and C2, which one gets used here?
}

Please, let me know. I know the above code works logically in C. But, want clarification on the assignment.

Invictus
  • 2,653
  • 8
  • 31
  • 50
  • It will store whatever the value of `val` is. – user253751 Feb 18 '16 at 02:53
  • I know that, but in what will it store? – Invictus Feb 18 '16 at 02:54
  • it will store it in `a1.store`...? – user253751 Feb 18 '16 at 03:13
  • In C, a1.store is basically an integer and your enums are essentially #defines with some extra compile time eye candy unless an enum value exceeds INTMAX (then it the smallest integer type that fits the max value) or you use a compiler specific "packed" attribute. C++ does a little bit more with typedef'ed enums and would probably give a warning/error for the above assignment if a something passes a value other than 1 or 0. – technosaurus Feb 18 '16 at 04:16

2 Answers2

3

What you are confusing is a1.store will store C1 or C2.

Actually, a1.store could be C1 or C2 or 2 or 3 or 255 or whatever is the value of val.

Back to the C standard, an enum variable can store a value that is out of the range of values of the enum type.

You could also refer this Enumeration object set to a value not equal to any of its respective enumeration constants

Community
  • 1
  • 1
Van Tr
  • 5,889
  • 2
  • 20
  • 44
  • And this is why it is not necessarily pointless to use `switch (a1.store) { case C1: …; case C2: …; default: …handle out of range…; }`. Even though the `default` case should never be executed, it might if `val` is not a value from the enumeration. – Jonathan Leffler Feb 18 '16 at 15:16
0

This is undefined behavior. The most likely result from your code is a crash, because you are dereferencing an uninitialized pointer.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • 1
    Thanks sam for answering. Please, can you help me clarify on assignment operation here on enums. – Invictus Feb 18 '16 at 02:52
  • This assignment operation is no different than any other assignment operation. It replaces the previous value of something, with a new value. That's it. – Sam Varshavchik Feb 18 '16 at 02:53
  • Yes but, in which enum? C1 or C2? as store as C1 and C2. – Invictus Feb 18 '16 at 02:55
  • 2
    There's only one enumerated value. You are confusing enumeration with structures. An enumeration is an alias. Instead of saying that this integer's values can only be 0, 1, or 2, an enumeration says that this integer's value can only be C1, C2, or C3, for example. Using C1, or C2, or C3 is exactly the same as using the underlying integer value assigned to each enumeration. – Sam Varshavchik Feb 18 '16 at 02:56
  • Thanks, that was a dumb question ;). Got confused. – Invictus Feb 18 '16 at 03:00
  • Note that the code in the question has been fixed since Sam posted his answer so that the correctly diagnosed 'undefined behaviour' is no longer a problem. – Jonathan Leffler Feb 18 '16 at 15:18