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

Give a one-line C expression to test whether a number is a power of 2.

10 Answers   Microsoft,


How do I write a program to print proper subset of given string . Eg :input: abc output:{},{a},{b},{c},{a,b},{a,c},{b,c}, {a,b,c}.I desperately need this program please mail me to saravana6m@gmail.com

11 Answers   Deshaw, Infosys,


Sorting entire link list using selection sort and insertion sort and calculating their time complexity

1 Answers   Infosys, Microsoft, NetApp,


Write a Program in 'C' To Insert a Unique Number Only. (Hint: Just Like a Primary Key Numbers In Database.) Please Some One Suggest Me a Better Solution for This question ??

0 Answers   Home,


I need your help, i need a Turbo C code for this problem.. hope u'll help me guys.? Your program will have a 3x3 array. The user will input the sum of each row and each column. Then the user will input 3 values and store them anywhere, or any location or index, temporarily in the array. Your program will supply the remaining six (6) values and determine the exact location of each value in the array. Example: Input: Sum of row 1: 6 Sum of row 2: 15 Sum of row 3: 24 Sum of column 1: 12 Sum of column 2: 15 Sum of column 3: 18 Value 1: 3 Value 2: 5 Value 3: 6 Output: Sum of Row 1 2 3 6 4 5 6 15 7 8 9 24 Sum of Column 12 15 18 Note: Your program will not necessary sort the walues in the array Thanks..

0 Answers  






char *someFun1() { char temp[ ] = “string"; return temp; } char *someFun2() { char temp[ ] = {‘s’, ‘t’,’r’,’i’,’n’,’g’}; return temp; } int main() { puts(someFun1()); puts(someFun2()); }

2 Answers  


Write a C function to search a number in the given list of numbers. donot use printf and scanf

5 Answers   Honeywell, TCS,


main() { int a=2,*f1,*f2; f1=f2=&a; *f2+=*f2+=a+=2.5; printf("\n%d %d %d",a,*f1,*f2); }

6 Answers  


main() { int i; clrscr(); for(i=0;i<5;i++) { printf("%d\n", 1L << i); } } a. 5, 4, 3, 2, 1 b. 0, 1, 2, 3, 4 c. 0, 1, 2, 4, 8 d. 1, 2, 4, 8, 16

4 Answers   HCL,


How to count a sum, when the numbers are read from stdin and stored into a structure?

1 Answers  


find simple interest & compund interest

2 Answers  


To Write a C program to remove the repeated characters in the entered expression or in entered characters(i.e) removing duplicates. String contains only lowercase characters ['a'-'z']

0 Answers  


Categories