What is the main difference between STRUCTURE and UNION?
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 x=5; clrscr(); for(;x==0;x--) { printf("x=%d\n”", x--); } } a. 4, 3, 2, 1, 0 b. 1, 2, 3, 4, 5 c. 0, 1, 2, 3, 4 d. none of the above
int DIM(int array[]) { return sizeof(array)/sizeof(int ); } main() { int arr[10]; printf(“The dimension of the array is %d”, DIM(arr)); }
Find your day from your DOB?
15 Answers Accenture, Microsoft,
Sir... please give some important coding questions asked by product companies..
How to palindrom string in c language?
main() { char *p; p="%d\n"; p++; p++; printf(p-2,300); }
1) int i=5; j=i++ + i++ + i++; printf("%d",j);This code gives the answer 15.But if we replace the value of the j then anser is different?why? 2)int i=5; printf("%d",i++ + i++ + i++); this givs 18.
write a program in c to merge two array
main() { int i=1; while (i<=5) { printf("%d",i); if (i>2) goto here; i++; } } fun() { here: printf("PP"); }
Give a very good method to count the number of ones in a 32 bit number. (caution: looping through testing each bit is not a solution)
main() { char c; int i = 456; clrscr(); c = i; printf("%d", c); } a. 456 b. -456 c. random number d. none of the above