C Programming - Structures, Unions, Enums - Discussion

Discussion Forum : Structures, Unions, Enums - General Questions (Q.No. 2)
2.
What is the similarity between a structure, union and enumeration?
All of them let you define new values
All of them let you define new data types
All of them let you define new pointers
All of them let you define new structures
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
49 comments Page 1 of 5.

Raju Naidu said:   1 decade ago
Structure,union,enum all are userdefined datatypes.

Let we take one senario for structure.

Suppose in my programm i've to take the 100 variables. If you want to define many variables of different datatype, instead of declaring all those variables individually just go through sructure.Just define the one structure and take all those variables into that structure.

Then if you want to access any variable, just take the structname and dot operator and variable name.then we can access those variables.

union also used for storing the different datatypes variables. But there is small difference between structure and union.i.e
In structures we can acess all those variables at a time because of they are sharing different memory locations.

But in unions whatever we declare in the union those all are sharing same memory that's why at a time we can access only one variable. enum also used for storing user defined variable

Ex: enum { married,divorced,unmarred}

By defaultley married is indexed by o,divorced is 1, unmarried is 2.

If you want to change this order you can change by using married=10 then divorced = 11, unmarried = 12.

Manmeet said:   9 years ago
Structures: Structure is denied as a collection of similar elements with different data types. The syntax of structure is :

struct book
{
int pages;
char name;
char author;
int edition;
}b;
Unions: It is a special data type which looks like a structure but are different from each other .
The syntax of union is :
union book
{
char name;
int pages;
int edition;
};
union book b;

The major difference between is there sizes.

In structures all the sizes of attributes are considered where as in case of unions only data type with max size is considered.

Enum: These are the special variables which can assume values which have been previously defined.

Syntax is:

enum month{jan=1, feb, march, april, may}.

enum month this_month;

This_month=feb;

Pradeep kumar said:   1 decade ago
Use of structure we can use multiple data types in array.
We can define structure, union and enum.

STRUCTURE:
It is a user define data type that have we use define variable of different type of data type. it have allocate individual memory to all the variables declared in the structure based on there data types

UNION:
It is same as structure but memory allocation is maximum of there data type. In union variable have share these memory.

ENUM:
It have have declared different data type. the value of data type start with 0 by default.

Ex. enum={a,b,c,d};
Here initialize the value of
a=0
b=1
c=2
d=4

But enum={a=6,b,c,d}
Here initialize the value of
a=6
b=7
c=8
d=9

Anu said:   1 decade ago
Difference between structure and union is that......

Structure
structure allocates individual memory to all the variables declared in the structure based on there data types

whereas.....

unions allocation the memory takes place based on the variable which occupies largest memory space based on there data type

for example

if in a union 2 variables with data types char and int are given then it allocates a memory of 4 byte in GCC since it occupies more space than char the same memory is shared for both of them

Vishu said:   8 years ago
@Kamal.

Linked list: list is a collection of elements. There are 2 ways of maintaining list in computer memory. The first way is to take an array for sorting elements of the list. But arrays have some disadvantage. Insertion and deletion of the element require more processing. The second way is implementing a list in a memory by using a self-referential structure.

Jiten said:   9 years ago
Structure are user define data types in which you can store more than one data type variable. It is similar to union. But in union you can access only one value at a time because in union memory allocated to only one member at a time. While in structure all of its member have its own memory. That's why structure is taking more memory then union.

Swapnil said:   1 decade ago
@Priya: In structure only collection of different type of variables which are binded in one unit but enum are they who allows us to assign a particular value from given set to variable.

ex:
enum days {MON=-1, TUE, WED, THU, FRI, SAT};

//This allows days to have val from MON,TUE, WED,THU,FRI,SAT
Whose vaule are from -1 to 4. As MON=-1,TUE=0.

Murty SVVSN said:   1 decade ago
An array holds a collection related data items that are all belongs to one particular data type, i.e a single name to all data items but the data items have different memory locations.

Ex. datatype array_name[size];
int array[5];
float a[10];
char a[5];

so on...

Ravendra Patel said:   1 decade ago
The working of structure and union is the same but the difference is in their memory allocation. The size of an structure is the summation of his all the member's size while the size of a union is the same as his largest member's size.

Urs Truely Naresh said:   9 years ago
Structure occupies fixed width of memory space, while Union takes the required memory based on the values.

Enum has a set of value to choose from and it can be part of either the Struct/Union or stand-alone.


Post your comments here:

Your comments will be displayed after verification.