What is Array in C Language?

Array in C Language

Array in C language are essential data structures that allow us to store multiple elements of the same data type. With arrays, we can efficiently manage and manipulate a collection of values, whether it’s a list of numbers, characters, or any other type. In this content, we will explore the fundamentals of arrays in C and learn how to utilize their power for effective programming.

Arrays are groups (or collections) of the same data type. For example, an int array contains one element of type int, and a float array contains one element of type float.

All arrays consist of contiguous memory locations. The minimum address corresponds to the first element and the maximum address corresponds to the last element.

Why do you need arrays?

If you have a small number of objects you can use regular variables (v1, v2, v3, ..), but if you store a large number of instances it will be difficult to manage with regular variables. The idea of an array is that a variable represents many instances.

Array declaration in C:

In C, you must declare the array correctly before you can use it by name and length. There are three syntaxes that allow you to declare an array in a C program.

Syntax 1:

int A[7] = {21,56,32,52,63,12,48} – Declaring the length and elements of array

For example:

Here Code:

#include<stdio.h>
int main{
int a[7] = {21,56,32,52,63,12,48};
int i;
for(i=0;i<7;i++){
printf("%d\n",a[i]);
}
return 0;
}

Syntax 2:

int  A[]={21,56,32,52,63,12,48} – Declaring the length of elements of array

For example:

Here Code:

#include<stdio.h>
int main{
int a[] = {21,56,32,52,63,12,48};
int i;
for(i=0;i<7;i++){
printf("%d\n",a[i]);
}
return 0;
}

Array initialization:

After declaring the array, it must be initialized. Otherwise, the garbage value (any random value) is included. Arrays can be initialized at compile time or at run time.

Array initialization at compile time:

The compile-time initialization of the elements of the array is the same as the initialization of a normal variable. A common form of array initialization is

data-type array-name[size] = { list of values };

One important thing to note is that if you specify more initializers (array elements) than the declared array size, the compiler will throw an error.

For example:

Here Code:

#include<stdio.h>
int main()
{
    int i;
    int arr[] = {2, 3, 4};// Compile time array initialization
    for(i = 0 ; i < 3 ; i++) 
    {
        printf("%d\t",arr[i]);
    }
}

Array initialization at run time:

Arrays can also be initialized at run time using the scanf () function. This approach is typically used to initialize large arrays or to initialize arrays with user-specified values.

For example:

Here Code:

#include<stdio.h>
int main()
{
    int arr[4];
    int i, j;
    printf("Enter array element");
    for(i = 0; i < 4; i++)
    {
        scanf("%d", &arr[i]);//Run time array initialization
    }
    for(j = 0; j < 4; j++)
    {
        printf("%d\n", arr[j]);
    }
}

Multidimensional Array in C Language:

In C, you can use multidimensional arrays, that is, arrays that can contain elements in both rows and columns.

Arrays generally focus linearly on a piece of information called one-dimensional. A dimension stores only one piece of information in the data, such as student negligence. In some situations, you may need to store data in a table format consisting of rows and columns or handle complex data. To visualize it, we need an array format called a two-dimensional array that requires image and graphics pixels for placement. The data is saved in tabular format. Matrix operations are performed by rearranging elements using functions such as reshape and squeeze.

Syntax:

The general declaration of a multidimensional array is given as follows:

data type name [ size] [size]……. N;

  • data type: indicates the element type (integer, floating point number).
  • name: shows the name assigned to the dimensional matrix.
  • Row size: some examples of row elements. If row size = 8, the array has 8 rows.
  • Column size: the number of column elements.

Types of Multidimensional array:

In C, Multidimensional array has three types:

1. Two-Dimensional Array:

A two-dimensional matrix is structured like a matrix and is implemented using rows and columns, also known as matrix arrays. Memory allocation is done in row or column measures. Also, the default format is Row-Major. When retrieving a two-dimensional array, each element is known to be either a one-dimensional array or a collection of one-dimensional arrays itself. The two-dimensional array uses two for loops or nested loops. The outer loop runs from 0 to the first subscript.

Syntax:

type array name [ no. of rows] [ no. of Columns];

For example:

Here Code:

#include<stdio.h>
int main ()
{
	int a[3][4], i, j;
for(i = 0; i < 3; i++)
{
for(j = 0; j < 4; j++)
	{
	printf("Enter arr[%d][%d]: ", i, j);
	scanf("%d", &a[i][j]);
	}
}
	printf("\nEntered 2-D array is: \n\n");
for(i = 0; i < 3; i++)
{
	for(j = 0; j < 4; j++)
	{	
	printf("%3d ", a[i][j] );
	}
	printf("\n");
}
	return 0;
}

2. Three-Dimensional Array:

This is called an array of array elements or array of arrays. It’s pretty weird, but practicing the logic makes it easier to implement. This 3D array requires more than 3 dimensions and requires a lot of memory to store.

Syntax:

data_type array_name [table name] [ no. of row] [ no. of column] int L[m][n] [p];

For example:

Here Code:

#include <stdio.h>
int main()
{
printf("Three dimensional array!\n\n");
int i, j, k, s[2][1][2], siz;
siz=2*1*2;
printf("Enter %d elements: \n",siz);
for(i = 0; i < 2; ++i)
{
for (j = 0; j < 1; ++j)
{
for(k = 0; k < 2; ++k )
{
scanf("%d", &s[i][j][k]);
}}}
printf("The stored values are:\n\n");
for(i = 0; i < 2; i++){
for (j = 0; j < 1; j++)
{
for(k = 0; k < 2; k++){
printf("sample[%d][%d][%d] = %d\n", i, j, k, s[i][j][k]);
}}}
}

3. Four-Dimensional Array:

This is a 3D matrix array, which is very difficult to manage. It is considered collectively as a pack of cubes and can be applied to spatial vectors.

Syntax:

Type array name [1][2][3][4] ……. [n] where 1,2 denotes the dimensions and n implies nth dimensions.

For example:

Here Code:

#include <stdio.h>
int main()
{
	int i, j, k, l, s=2;
	int d[2][2][2][2];
	d[0][0][0][0] = 4;
	d[0][0][0][1] = 3;
	d[0][0][1][0] = 2;
	d[0][0][1][1] = 6;
	d[0][1][0][0] = 6;
	d[0][1][0][1] = 8;
	d[0][1][1][1] = 2;
	d[1][0][0][0] = 6;
	d[1][0][0][1] = 9;
	d[1][0][1][0] = 5;
	d[1][0][1][1] = 1;
	d[1][1][0][0] = 9;
	d[1][1][0][1] = 7;
	d[1][1][1][0] = 5;
	d[1][1][1][1] = 7;
	for (i = 0; i < s; i++) {
	for (j = 0; j < s; j++) {
	for (k = 0; k < s; k++) {
	for (l = 0; l < s; l++) {
	printf("Value of stdio[%d][%d][%d][%d]: %d ", i, j, k, l, d[i][j][k][l]);
	printf("\n");
	}}}}
	return 0;
	}

Memory Allocation of Array in C Language:

The memory representation in C is divided into the following five sections.

  1. Text segment
  2. Initialized data segment.
  3. Uninitialized data segment
  4. Stack
  5. Pile

The data, the heap, and the stack, like any other variable, are three segments in which you can allocate memory to store your elements in an array.

  1. Dynamic array: A dynamic array is an array and must be assigned a memory location at run time. For these types of arrays, memory is allocated in the memory location of the heap.
  2. Global or Static Arrays: These are the types of arrays that are allocated at compile time. Therefore, data segment memory is always allocated for these types of arrays.
  3. Local array: An array that is initialized within a function or block is called a local array. These types of od arrays get the memory allocated to the stack segment.

Advantages of Array in C Language:

  • Arrays use a single name to represent multiple data items of the same type.
  • Arrays allow you to randomly access elements using index numbers.
  • The array allocates memory in contiguous memory locations for all of its elements. Therefore, for arrays, no additional memory can be allocated. This prevents memory overflows and memory shortages on the array.
  • Arrays allow you to implement other data structures such as linked lists, stacks, queues, trees, and charts.
  • Two-dimensional matrices are used to represent matrices.

Disadvantages of Array in C Language:

  • You need to know in advance how many elements will be stored in the matrix.
  • The matrix has a static structure (that is, the size of the matrix is ​​fixed). Once declared, the array cannot be resized. You cannot increase or decrease the allocated memory.
  • Inserts and deletes in arrays are very difficult because elements are stored in contiguous memory locations and change operations are expensive.
  • Allocating more memory than necessary wastes memory space and causes problems when less memory is allocated.

Conclusion:

Arrays are a type of data structure that is used to store similar types of data in contiguous memory locations. Programming arrays are used to represent various complex data structures, such as trees and heaps. The C language allows multidimensional arrays of all primitive data types. The string is also represented as an array of strings with the null character “\ 0” as the last character. Programming arrays allow you to quickly retrieve and directly access the elements of an array using the index that contains the elements.

Learn More About : Polymorphism in C Plus Plus

References:

Leave a Comment

Your email address will not be published. Required fields are marked *