Difference Between Array And Union In C

A collection of identical data elements grouped together under a single identifier and stored in separate memory locations is known as an array. Accessing specific elements within an array is achieved by using indices. It is crucial to emphasize that all elements within an array must share a common data type. Populating an array necessitates the utilization of primitive data types such as integers, floating-point numbers, doubles, characters, etc., all of which must adhere to the same data type.

Types of Arrays in C:

C encompasses two distinct types of arrays:

  • One-Dimensional Arrays: A fundamental array type in C, a one-dimensional array consists of elements of the same type accessible through their respective indices.
  • Multi-Dimensional Arrays: The prevalent multi-dimensional array in C is the 2D array. The number of dimensions can exceed two based on the language of the system. These arrays are composed of array elements.
  • Array declaration in C

In the C programming language, it is necessary to specify the array before utilizing it, similar to other types of variables. An array is declared by specifying its name, the data type of its elements, and the total number of elements it can hold. Upon creating an array in C, the compiler assigns a memory block based on the specified size to the array's name.

Syntax:

It has the following syntax:

Example

datatype array_name[length]

Initialization of an Array in C

In the C programming language, initialization refers to the act of assigning an initial value to a variable. When an array is declared or memory is allocated for it, the elements within the array may contain random or garbage values. Therefore, it becomes necessary to assign meaningful values to the array elements. In C, there are multiple methods available for initializing an array.

  1. Array Declaration and Initialization

In this procedure, we start by setting up the array and defining it. An initialization list is employed to set values for multiple array elements. It consists of values enclosed in curly braces and separated by commas.

Syntax:

It has the following syntax:

Example

datatype array_name[length]={v1,v2…..vn};
  1. Array declaration without size

When initializing an array using an initializer list, it is not necessary to explicitly specify the size of the array as the compiler can automatically deduce it. The size of the array will match the number of elements in the initializer list due to the compiler's ability to infer the array's size.

Syntax:

It has the following syntax:

Example

datatype name[] = {10,20,30,40,50};

What is the Union?

A union is a custom data type that enables the storage of various items within a single memory location. While a union can contain multiple members, only one member can store a value at a time. Unions are ideal for scenarios where multiple data types need to share the same memory space effectively.

Declaration of Union

Syntax:

It has the following syntax:

Example

union name_union

{

 datatype value;

 datatype value;

};

C unions enable the sharing of memory space among exclusive data members, which is particularly valuable in scenarios with limited memory like embedded systems. They are commonly employed in embedded applications that necessitate direct memory manipulation.

Key differences between Array and Union

There exist multiple variances between the Array and Union in the C programming language. Some key distinctions between the array and union are outlined below:

Aspect Array Union
Purpose Arrays are used to hold numerous elements of the same data type in contiguous memory regions. Unions allow multiple data kinds to share the same memory space while only allowing one member to be functional at a time.
Datatype Arrays can only carry items of one data type. For example, an array containing numbers, characters, etc. Unions can store components of many data kinds. For example, a union may include an integer, a float, and a character array.
Memory Allocation Arrays allocate memory that is contiguous to all items of the same data type. Each of the members of a union shares a single memory location, and the largest member determines the union's size.
Accessing Elements Elements are accessible using indices that begin at 0. In a union, the most recently allocated member can be retrieved.
Size Declaration The size of the arrays must be defined at compile time and must stay constant during execution. The largest member of a union determines its size.
Memory Efficiency Each array member has its own memory area, which may result in greater memory use. Unions are memory-efficient because members share an identical memory area, but they are limited in continuous usage.
Use Cases Arrays are suitable for storing collections of comparable data types when access to elements by an index is required, such as a list of numbers or characters. Unions enable for memory optimization in situations when many data types must share a memory region. For example, they may be used to represent a value that can be an integer, float, or character array-but only a single at a time.

Input Required

This code uses input(). Please provide values below: