-2

I have a union having two struct variables. the struct contains a string (char array).

Here's my code:

#include <stdio.h>
#include <string.h>

int main(void) {
    struct name{
        char name_v[50];
    };

    union myunion{
        struct name a;
        struct name b;
    }x;

    strcpy(x.a.name_v, "HELLO PEEPS");

    printf("%s\t%s", x.a.name_v, x.b.name_v);

    return 0;
}

Since a union allocates enough memory to hold the highest value at a given time, I thought that the name_v of only struct name a will hold the value "HELLO PEEPS" and that of struct name b will hold '\0'.

However, the output of the above code is

HELLO PEEPS        HELLO PEEPS

But I expected it to show something else (null?) in place of the second HELLO PEEPS.

So it seems that both the members of the union are being assigned the same value at the same time. (???)

• Why is this happening?
• Why am I not getting the expected output?
• What am I doing wrong that's not getting me to it?

I expect one of the union members to bear a null value when the other holds some valid value and want to be able to check that. Please help me to achieve this.

progyammer
  • 1,498
  • 3
  • 17
  • 29

1 Answers1

2

Unions allow you to place data in the same memory location.

They are also only as big as the biggest member.

You store "HELLO PEEPS" in one struct, and nothing in the other.. then reading out one will give you the same as the other. ( They start at the same memory address ).

Burstful
  • 347
  • 1
  • 10
  • So this is going to be the case when we have members of the same data type, right? Because I've seen that when I have a string and an int inside a union, only one of them will have a legit value at a time. – progyammer Oct 17 '17 at 18:15
  • When you have a "string" do you mean your char name_v[50]? All the union does is allocate memory only the size of the biggest member, and when assigning to any member, the other members are subject to change ( since they are all at the same memory address ). – Burstful Oct 17 '17 at 18:19
  • I actually understood how it works _(kind'a)_: Suppose we have two strings. In that case, both pointing to the same memory location will yield the same string value _(which happened in my case)_. But if I have an int and a String then both pointing to the same memory location will yield the value only if compatible, i.e., if the address stores a string then only the string variable will yield that and referring to the int variable will yield some random things _(Maybe the memory address)_, right?? – progyammer Oct 17 '17 at 18:36
  • If you put a string of characters into the union member `name_v[50]` and the other member was an int, you would get inside that integer the first letter or two (depending on int size which is platform speicfic) of the string. – Burstful Oct 17 '17 at 18:38
  • letter inside integer??? – progyammer Oct 17 '17 at 18:40
  • 1
    Yes. This is C, you will get the byte representation of the ascii code for the letters .. in your int. – Burstful Oct 17 '17 at 18:41