What is the main difference between STRUCTURE and UNION?

Answers were Sorted based on User's Feedback



What is the main difference between STRUCTURE and UNION?..

Answer / vijay nag

All the members of the structure can be accessed at
once,where as in an union only one member can be used at a time.
Another important difference is in the size allocated to a
structure and an union.
for eg:
struct example
{
int integer;
float floating_numbers;
}
the size allocated here is sizeof(int)+sizeof(float);
where as in an union
union example
{
int integer;
float floating_numbers;
}
size allocated is the size of the highest member.
so size is=sizeof(float);

Is This Answer Correct ?    802 Yes 65 No

What is the main difference between STRUCTURE and UNION?..

Answer / ravi

1) Structure: The size in bytes is the sum total of size of
all the elements in the structure, plus padding bytes.
2) Size of in bytes of the union is size of the largest
variable element in the union.

i.e In case of Union, the elements making up the
union 'overlap' in memory OR they are accessed as diffrent
name/type at diffrent places in the program.

Whereas in case of Struct, each of the elements have a
distinct identity.

Is This Answer Correct ?    338 Yes 60 No

What is the main difference between STRUCTURE and UNION?..

Answer / dinesh haridoss

The difference between structure and union in c are: 1.
union allocates the memory equal to the maximum memory
required by the member of the union but structure allocates
the memory equal to the total memory required by the
members. 2. In union, one block is used by all the member
of the union but in case of structure, each member have
their own memory space

Is This Answer Correct ?    295 Yes 29 No

What is the main difference between STRUCTURE and UNION?..

Answer / om prakash mit

All the members of the structure can be accessed at
once,where as in an union only one member can be used at a
time.

Is This Answer Correct ?    193 Yes 28 No

What is the main difference between STRUCTURE and UNION?..

Answer / priti

. union allocates the memory equal to the maximum memory
required by the member of the union but structure allocates
the memory equal to the total memory required by the
members.
2. In union, one block is used by all the member of the
union but in case of structure, each member have their own
memory space
1. Let’s say a structure containing an int, char and
float is created and a union containing int char float are
declared. struct TT{ int a; float b; char c; } Union UU{
int a; float b; char c; }
2. sizeof TT(struct) would be >9 bytes (compiler
dependent-if int,float, char are taken as 4,4,1)
3. sizeof UU(Union) would be 4 bytes as supposed from
above.If a variable in double exists in union then the size
of union and struct would be 8 bytes and cumulative size of
all variables in struct

Is This Answer Correct ?    90 Yes 21 No

What is the main difference between STRUCTURE and UNION?..

Answer / puneet shukla

in union the size allocated is the size of the highest
member whereas in structure the size allocated is the sum of
the size of all its declerations.

Is This Answer Correct ?    69 Yes 21 No

What is the main difference between STRUCTURE and UNION?..

Answer / asesh k tripathy

The difference between union and structure can be as follows:
1. The way structure occupies memory for its member is different from union.
(a) Structure occupies appropriate separate memory for its members
(b) Union occupies memory for that member which needs largest chunk of bytes.
2. We can initialize any of the structure members while initializing any of the union member other than the first member may have unpredictable results.

Is This Answer Correct ?    57 Yes 21 No

What is the main difference between STRUCTURE and UNION?..

Answer / harshita

While structure enables us treat a number of different variables stored at different in memory , a union enables us to treat the same space in memory as a number of different variables. That is a Union offers a way for a section of memory to be treated as a variable of one type on one occasion and as a different variable of a different type on another occasion.

Union allocates the memory equal to the maximum memory required by the member of the union but structure allocates the memory equal to the total memory required by the members.

Union allocates the memory equal to the maximum memory required by the member of the union but structure allocates the memory equal to the total memory required by the members.

Example:

union exforsys_t {
char c;
int i;
float f;
} exforsys;


Defines three elements:

exforsys.c
exforsys.i
exforsys.f

Each one with a different data type. Since all of them are referring to the same location in memory, the modification of one of the elements will affect the value of all of them. We cannot store different values in them independent from each other.

Is This Answer Correct ?    41 Yes 22 No

What is the main difference between STRUCTURE and UNION?..

Answer / hari krishna

1) All the members of the structure can be accessed at once but, in case of union its different only one member can be can be used at a time.................                                                                                 2) In union the size allocated is the size of the highest
member whereas in structure the size allocated is the sum ofthe size of all its declerations........

Is This Answer Correct ?    18 Yes 3 No

What is the main difference between STRUCTURE and UNION?..

Answer / anil kumar nahak

1.)A union is a way of providing an alternate way of
describing the same memory area. In this way, you could
have a struct that contains a union, so that the "static",
or similar portion of the data is described first, and the
portion that changes is described by the union. The idea of
a union could be handled in a different way by having 2
different structs defined, and making a pointer to each
kind of struct. The pointer to struct "a" could be assigned
to the value of a buffer, and the pointer to struct "b"
could be assigned to the same buffer, but now a->somefield
and b->someotherfield are both located in the same buffer.
That is the idea behind a union. It gives different ways to
break down the same buffer area.

2.)The difference between structure and union in c are: 1.
union allocates the memory equal to the maximum memory
required by the member of the union but structure allocates
the memory equal to the total memory required by the
members. 2. In union, one block is used by all the member
of the union but in case of structure, each member have
their own memory space

3.)Detailed Example:
struct foo
{
char c;
long l;
char *p;
};

union bar
{
char c;
long l;
char *p;
};

A struct foo contains all of the elements c, l, and p. Each
element is
separate and distinct.

A union bar contains only one of the elements c, l, and p
at any given
time. Each element is stored in the same memory location
(well, they all
start at the same memory location), and you can only refer
to the element
which was last stored. (ie: after "barptr->c = 2;" you
cannot reference
any of the other elements, such as "barptr->p" without
invoking undefined
behavior.)

Is This Answer Correct ?    42 Yes 29 No

Post New Answer

More C Code Interview Questions

main() { if (!(1&&0)) { printf("OK I am done."); } else { printf("OK I am gone."); } } a. OK I am done b. OK I am gone c. compile error d. none of the above

3 Answers   HCL,


Is the following statement a declaration/definition. Find what does it mean? int (*x)[10];

1 Answers  


Print an integer using only putchar. Try doing it without using extra storage.

2 Answers  


main() { int i=10; i=!i>14; Printf ("i=%d",i); }

1 Answers  


char *someFun() { char *temp = “string constant"; return temp; } int main() { puts(someFun()); }

1 Answers  






main() { main(); }

1 Answers  


int swap(int *a,int *b) { *a=*a+*b;*b=*a-*b;*a=*a-*b; } main() { int x=10,y=20; swap(&x,&y); printf("x= %d y = %d\n",x,y); }

1 Answers  


Given a list of numbers ( fixed list) Now given any other list, how can you efficiently find out if there is any element in the second list that is an element of the first list (fixed list)

3 Answers   Disney, Google, ZS Associates,


main() { printf("%d, %d", sizeof('c'), sizeof(100)); } a. 2, 2 b. 2, 100 c. 4, 100 d. 4, 4

18 Answers   HCL, IBM, Infosys, LG Soft, Satyam,


main() { int y; scanf("%d",&y); // input given is 2000 if( (y%4==0 && y%100 != 0) || y%100 == 0 ) printf("%d is a leap year"); else printf("%d is not a leap year"); }

1 Answers  


1 o 1 1 0 1 0 1 0 1 1 0 1 0 1 how to design this function format in c-language ?

2 Answers  


Write a C program that defines a 2-dimentional integer array called A [50][50]. Then the elements of this array should randomly be initialized either to 1 or 0. The program should then print out all the elements in the diagonal (i.e. a[0][0], a[1][1],a[2][2], a[3][3], ……..a[49][49]). Finally, print out how many zeros and ones in the diagonal.

2 Answers  


Categories