Programmer Coding

Array in C Language

What is Array?

An array is defined as a collection of similar data elements stored in contiguous memory locations. Array is a data type provided by C language, it can be int, char, double, float etc. It can store primitive data such as In the simplest case, any data point can be accessed using index numbers.

C array is useful if similar objects need to be stored. For example, if we want to keep the student’s scores in 6 subjects, we do not need to define the difference between the scores in different subjects. Instead, we can define an array that stores the labels of each element in the unified memory space.

We can easily access elements using arrays. Accessing the contents of a directory requires only a few lines of code.

Properties of Array in C

In C, an array is a large collection of objects stored in contiguous memory. Arrays are a simple data type in C and can store the contents of many data types, such as numbers, symbols, and custom formulas. They are widely used by programmers to solve various problems in C and other programming languages.

The properties and behavior of arrays may differ from programming language to programming language. In this article, we will examine the special properties and operations of arrays in the C programming language.

 

  1. Fixed Size Collection
  2. Homogeneous Elements
  3. Indexing in Array
  4. Dimensions of Array
  5. Contiguous Storage
  6. Random Access
  7. Array name relation with pointer
  8. Bound Checking
  9. Array Decay

1. Fixed Size of an Array

In C, the size of an array is set at declaration time and cannot be changed at runtime. This dimension must be taken into account during assembly and regular storage.

Example

// C Program to Illustrate the Fixed Size Properties of the

// Array

#include <stdio.h>

int main()

{

// creating a new array of size 5

int array[5] = { 1, 2, 3, 4, 5 };

printf(“Size of Array Before: %d\n”,

sizeof(array) / sizeof(int));

// trying to increase the size of the array

array[6];

// not checking the size

printf(“Size of Array After: %d”,

sizeof(array) / sizeof(int));

return 0;

}

Output

Size of Array Before: 5

Size of Array After: 5

1.  Homogeneous Collection

Arrays in C do not contain data elements of different types, but are of the same type.

Syntax

data_type array_name[size];

Example

#include <stdio.h>

int main() {

// Array of integers

int numbers[5] = {1, 2, 3, 4, 5};

return 0;

}

3. Indexing in an Array

In C, the index of elements in an array starts from 0, not 1. This means that the first element in the array has an index of 0, and the last element has an index of equal magnitude, less than the number minus 1. If you have an array of size n, the valid index of the array is 0 to n – 1.

Syntax

array_name[index];

Example

#include <stdio.h>

int main() {

int numbers[5] = {1, 2, 3, 4, 5};

printf(“Element at index 2: %d\n”, numbers[2]);

return 0;

}

Output

Element at index 2: 3

4. Dimensions of the Array

In C, arrays can take many forms. They can be one-dimensional (such as 1-dimensional arrays) or multi-dimensional (such as 2-dimensional, 3-dimensional arrays). Multidimensional arrays can have many dimensions, allowing you to represent complex data.

The number of elements in a multidimensional array is calculated by multiplying the dimensions of each dimension. For example, in a two-dimensional array of length m and n, all elements are m * n. In a 3-dimensional array with dimensions x, y, and z, all elements are x*y*z, and so on for larger dimensions.

array size allows C programmers to use and handle more data types.

Example

// C Program to create multidimensional array

#include <stdio.h>

int main()

{

// creating 2d array

int arr2d[2][2] = { 1, 2, 3, 4 };

// creating 3d array

int arr3d[2][2][2] = { 1, 2, 3, 4, 5, 6, 7, 8 };

printf(“2D Array: “);

// printing 2d array

for (int i = 0; i < 2; i++) {

for (int j = 0; j < 2; j++) {

printf(“%d “, arr2d[i][j]);

}

}

printf(“\n3D Array: “);

// printing 3d array

for (int i = 0; i < 2; i++) {

for (int j = 0; j < 2; j++) {

for (int k = 0; k < 2; k++) {

printf(“%d “, arr3d[i][j][k]);

}

}

}

return 0;

}

Output

2D Array: 1 2 3 4

3D Array: 1 2 3 4 5 6 7 8

5. Contiguous Storage

In C language, all elements in an array are stored in a linked location or memory location. This concept is easy to see in the case of a one-dimensional array, where elements are only placed one after the other in memory. However, it is worth noting that even many arrays are stored in memory.

Constant storage of multiple arrays is used in main row or main column order. In main line order, the contents of each line are stored in memory, then the next line, etc. The contents of each column are stored according to the column size.

You can control the union of arrays in C by using pointers to access and iterate over the contents in memory. By incrementing a pointer to an array, you can verify that the array is persistent by checking whether it points to a consecutive element in memory.

Example

// C Program to Verify the Contiguous Storage of Elements in

// an Array

#include <stdio.h>

int main()

{

// creating an array of 5 elements

int arr[5] = { 1, 2, 3, 4, 5 };

// defining pointers to 2 consecutive elements

int* ptr1 = &arr[1];

int* ptr2 = &arr[2];

// printing the address of arr[1] and arr[2]

printf(“Address of arr[1] : %p\n”, ptr1);

printf(“Address of arr[2] : %p”, ptr2);

return 0;

}

Output

Address of arr[1] : 0x7fffb8cc1ef4

Address of arr[2] : 0x7fffb8cc1ef8

6. Random Access to the Elements

A defining and powerful feature of arrays in C is the ability to access each element using its own index. These features are a direct result of persistent storage, where all content is stored in memory.

The compiler can use the address and index number of the first element to determine the address of the element at the given index. This calculation provides efficient and direct access to all array positions without skipping other elements. This random access feature is essential for many algorithms and data models that rely on fast and direct content retrieval.

Syntax

Address of ith = Address of 1st Element + (Index * Size of Each Element)

Example

// C Program to check the random access property of the

// array

#include <stdio.h>

int main()

{

// creating an array of 5 elements

int arr[5] = { 1, 2, 3, 4, 5 };

// address of first element

int* ptr = &arr[0];

// printing arr[3]

printf(“Array[3]: %d\n”, arr[3]);

// printing element at index 3 using ptr

printf(“Array[3] using pointer to first element = %d”,

*(ptr + 3));

return 0;

}

Output

Array[3]: 4

Array[3] using pointer to first element = 4

7. Relationship between Array and Pointers

Arrays in C are closely related to pointers, and in fact most operations that can be performed on arrays can also be done using pointers. In fact, the name of the array is a pointer to its first element. This relationship allows you to use metric arithmetic to navigate and manipulate array elements.

Example

// C Program to Illustrate the Relationship Between Array

// and Pointers

#include <stdio.h>

int main()

{

// creating an array with 3 elements

int arr[3] = { 1, 2, 3 };

 

int* ptr = &arr[0];

// Pointer to first element

printf(“Pointer to First Element: %p\n”, ptr);

// Array name as pointer

printf(“Arran Name: %p”, arr);

return 0;

}

Output

Pointer to First Element: 0x7ffec5059660

Arran Name: 0x7ffec5059660

8. Bound Checking

In C, bounds checking (which involves checking whether the input element is within the declaration of the array) is not done by the language itself. This means that C allows you to access elements outside the scope of an array without throwing an exception or exception.

Although this change can be used in some cases, it also brings some risks. Accessing elements outside array boundaries can cause bad behavior and errors in your program, including memory corruption and vulnerabilities. It is the C programmer’s responsibility to ensure that array boundaries are not violated and to apply manual cross-checking when necessary to avoid such problems.

Example

// C Program to Illustrate the Out of Bound access in arrays

#include <stdio.h>

int main()

{

// creating new array with 3 elements

int arr[3] = { 1, 2, 3 };

// trying to access out of bound element

printf(“Some Garbage Value: %d”, arr[5]);

return 0;

}

Output

Some Garbage Value: 0

9. Array Decay

Array corruption is the phenomenon where an array loses its size and becomes a pointer under certain conditions. One of the situations where array corruption occurs is when an array becomes an argument to a function. When an array becomes an index, you cannot determine its size using the sizeof() operator because the data size is lost.

Example

// C Program to Demonstrate the Array Decay

#include <stdio.h>

// function

void func(int* arr)

{

printf(“Sizeof Value in Function: %d”, sizeof(arr));

}

int main()

{

// creating array with 3 elements

char arr[3];

printf(“Sizeof Value in Main: %d\n”, sizeof(arr));

// passing array

func(arr);

return 0;

}

Output

Sizeof Value in Main: 3

Sizeof Value in Function: 8

Types of Array in C

  • One dimensional Array(1D)
  • Multi Dimensional Array(2D)

Leave a Comment

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

Scroll to Top