Differences Programming

Difference Between Array and Union in C

What is an Array?

An array is a collection of data items, all of the same types, accessed using a common name. A one-dimensional array is like a list; A two-dimensional array is like a table; The C language places no limits on the number of dimensions in an array, though specific implementations may.

What is the Union?

A union is a special data type available in C that allows storing different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time.

Read more: Difference between Microprocessor and Microcontroller

Difference Between Array and Union in C

Array Union
An array is the collection of data items which will have the same name. The union is a collection of heterogeneous data types.
Each element has a specific memory. Each element has no specific memory space but space allocated for the largest element is utilized by another sized element.
The keyword is not required to declare an array. The keyword is not required to declared union
The array has its type and these are. One dimensional array and multi-dimensional array Union has not typed.
The syntax of array: One dimensional array; Data type_array name[size of array]
It content only rows or columns.
Eg. Char name[20];
Multi-dimensional array;
Data type_array name[size1][size2];
Eg. Char name [5][25];
It contents rows or column both at a time.
Syntax of union:
Union user_defined_name{
Data type member 1;
Data type member 2;
.
.
};
Union user defined name
Variable 1, variable2
e.g struct student {
char name [20];
int age;
};
Union student one;

Tuts

About Author

Tutsmaster.org provides tutorials related to tech and programmings. We are also setting up a community for the users and students.