Confused about difference between structure and union
Hi All,
I am one of the beginner in the field of programming. I want your help to understand the difference between structure and union. For me the Structure and Union seems similar, I am not able to differentiate between them.
Do you know any difference between structure and union?
Can able to provide me the example which show the difference between structure and union?
Re: Confused about difference between structure and union
Hi,
Following are the main difference between between structure and union of C language:
A. Every member of "structure" has it's own space of memory, While in "Union" all member use only single block of memory.
B. Only required memory is reserved to member of the "structure", While in the "Union" max required memory is being allocated to the member of "Union"
Re: Confused about difference between structure and union
You can access all structure members at single use, On other hand in Union you can access only one union member at single time. This is the only difference which is I know. Following is the example of Structure and Union:
Union:
Quote:
union demo
{
int integer_num;
float float_num;
}
Structure:
Quote:
struct demo
{
int integer_num;
float float_num;
}
Re: Confused about difference between structure and union
Hi Friend,
I know the concept of structure very well, But don't have sound knowledge about Union. Structure is works as user defined data types in C programming.The only difference between them is members in the structure individually use block of memory ,but Union members use only one common space.
Refer below example:
Quote:
struct emp
{
long o;
char d;
};
Here variable "o" is of type long and variable "d" is of type character.