EmbLogic's Blog

Can i initialize an union members like structure???if yes then why i am getting so much warning in the following programm????

#include<stdio.h>

union logic
{

int i;
char x[2];
//char y;

}emb={510,’A’,’B’};
int main()
{

printf(“%d\n”,emb.i);
printf(“%c\n”,emb.x[0]);
printf(“%c\n”,emb.x[1]);
printf(“%d\n”,emb.i);
}

One Response to Can i initialize an union members like structure???if yes then why i am getting so much warning in the following programm????

  1. hemant.kumar says:

    Union is like a chunk of memory that is used to store variables of different types. Once a new value is assigned to a field, the existing data is wiped over with the new data.
    as far as i know you have to initialize each value separately
    #include
    #include

    union logic
    {
    int i;
    int x;
    char j[12];
    }emb;

    int main()
    {
    emb.i=10;
    // emb.x=12;
    printf(“value is %d \n”,*(&(emb.i)));
    emb.x=12;
    printf(“address of i is %p and x is %p \n”,&(emb.i),&(emb.x));
    printf(“value is %d \n”,*(&(emb.x)));

    strcpy(emb.j,”hello world”);
    printf(“%s\n”,emb.j);
    printf(“address is %p \n”,&(emb.j));
    return 0;
    }
    but ask SIR about this concept
    ~

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>