Array in C

An array is a special variable that is used to store multiple values of similar types(homogeneous) at contiguous memory locations.

Example:

Let’s say we have 10 students in a batch who want to store their marks. We can see that the marks of each student are of the same type that is an integer/float. Thus an array can be used as a data structure to store these marks.


Declaration of an array:

Like any other variable, arrays must be declared before they are used. Two important aspects of an array must be known in advance:

  • The data type of values to be stored in that array and
  • The number of values to be stored that is the size of an array.

Generic Syntax:

data-type array-name[size]; 

Examples:

int stud_marks[5];
char stud_name[5];
float emp_salary[5];

Initialization of an array:

Initialization is nothing but setting values to an array otherwise it may consist of garbage values (any random value). An array can be initialized at either compile-time or runtime.

Compile Time Array Initialization:

Compile time initialization of array elements is the same as ordinary variable initialization.

Generic Syntax:

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

Examples:

int stud_marks[5] = {10, 20,30,40, 50};
char stu_name[10] = {techaccess};
char stu_name1[10] = {'t','e','c','h','a','c','c','e','s','s','\0'};
float emp_salary[5] = {101.6, 119.7, 120.6, 123.8, 129.7};

Runtime Array Initialization:

An array can also be initialized at run time using scanf(). This approach is primarily used to initialize large arrays, or to initialize arrays with user specified values.

Example:

#include <stdio.h>

int main ()
{
  int arr[4];
  int i;
  for (i = 0; i < 4; i++)
     scanf("%d", &arr[i]);
  for (i =0;i< 4; i++)
     printf("%d\n", arr[i]);

   return 0;
}

Notes:

  • While declaring the array size must be specified to avoid any compilation error.
    int arr[ 5];
  • Declaration and initialization can be done at the same time.
    int stud_arry[5] = { 10, 20, 30, 40, 50};
  • If declaration and initialization are done at the same time, the size of the array is not mandatory.
    int stud_arry[] = { 10, 20,30,40,50};
  • The number of elements in an array can be less than the size of an array. The rest can contain garbage or random values.
    int stud_arry[5] = { 10, 20, 30};
  • The number of elements cannot be more than the size of an array. This leads to compilation errors.
    int stud_arry[5] = { 10, 20, 30, 40, 50, 60, 70, 80};
  • All the elements can be initialized to 0 at once, without individually setting it to 0.
    int stud_arry[5] = {0};

Accessing the Array elements:

In the case of an array, the elements are located at a contiguous memory location. All memory locations have a common name, so accessing individual memory locations is not possible directly.

Hence, the compiler not only allocates memory but also assigns a numerical reference value to the individual memory location of an array. This is termed index or subscript.

The individual elements of an array are identified using the combination of array-name and its index.

Generic Syntax:

array-name[index]

The index starts with 0 until it reaches the max (size – 1).

Example:

#include <stdio.h>
int main()
{
   /* Array declaration and initialization */
   int marks_array[10] = { 10, 11, 12, 13, 14};
   for (int i= 0; i < 5; i++)
     printf("\n Element at position %d is %d",i + 1, marks_array[i]);
   printf("\n Element at 4th index is %d", marks_array[3]);
   return 0;   
 }

OUTPUT: 

Element at position 1 position is 10
Element at position 2 is 11
Element at position 3 is 12
Element at position 4 is 13
Element at position 5 is 14
Element at 4th index is 13

Memory Representation of Array:

  • The array contains similar types of data and
  • Elements are stored at a contiguous memory location.

The first point we saw above is that all elements are of the same type. In the below section we will see how they are stored in memory.

#include <stdio.h>
int main()
{
  int marks_array[10] = { 10, 11, 12, 13, 14};
  for (int i= 0; i < 5; i++)
    printf("\n The address of the position: %d is: %p", i + 1, &marks_array[i]); 
 }

OUTPUT: 

 The address of the position is: 1 is: 0x7ffd94413a20
 The address of the position is: 2 is: 0x7ffd94413a24
 The address of the position is: 3 is: 0x7ffd94413a28
 The address of the position is: 4 is: 0x7ffd94413a2c
 The address of the position is: 5 is: 0x7ffd94413a30

We can see that there is a difference of 4 among each address. Hence they are stored in a sequential manner that is the contiguous manner in memory.


The base address of the array:

As we have seen, array elements are accessed by their index and are stored in a sequential manner in memory. Thus the address of the 1st element of an array is nothing but its base address.
Hence we can access any element of the array by using their base address by using arithmetic operators that is only plus and minus.


Let us see an example to illustrate the above concepts:

#include <stdio.h>
int main()
{
   int marks_array[10] = { 10, 11, 12, 13, 14};
   printf("\n Base address is %p", marks_array);
   printf("\n Address of 2nd element is : %p", marks_array + 1);
   printf("\n Address of 3rd element is : %p", marks_array + 2);
   printf("\n Address of 4th element is : %p", marks_array + 3);
   printf("\n Address of 5th element is : %p", marks_array + 4);
   
   printf("\n Value at base address is: %d",*marks_array);
   printf("\n Value at 2nd address is: %d",*(marks_array + 1));
   printf("\n Value at 3rd address is: %d",*(marks_array + 2));
   printf("\n Value at 4th address is: %d",*(marks_array + 3));
   printf("\n Value at 5th address is: %d",*(marks_array + 4));
   
   int *p = marks_array + 3; 
   printf("\n Address of 4th element is : %p", p);
   printf("\n Address of 3rd element is : %p", p -1);
   printf("\n Value at 3rd address is : %d",*(p - 1));
   return 0;
 }

OUTPUT:

Address of 2nd element is : 0x7fff71f61904
Address of 3rd element is : 0x7fff71f61908
Address of 4th element is : 0x7fff71f6190c
Address of 5th element is : 0x7fff71f61910
Value at base address is: 10
Value at 2nd address is: 11
Value at 3rd address is: 12
Value at 4th address is: 13
Value at 5th address is: 14
Address of 4th element is : 0x7fff71f6190c
The address of the 3rd element is : 0x7fff71f61908
Value at 3rd address is : 12

Access the array with pointers:

The element of an array can also be accessed using pointers. Please refer pointer for your understanding of the section below.

#include <stdio.h>
int main()
{
   int marks_array[10] = { 10, 11, 12, 13, 14};
   printf("\n Base address is %p", marks_array);
   printf("\n Address of 2nd element is: %p", marks_array + 1);
   printf("\n Value of 2nd element is: %d", *(marks_array + 1));
   return 0;
 }

OUTPUT:  

Base address is 0x7ffdb31785d0
Address of 2nd element is: 0x7ffdb31785d4
Value of 2nd element is: 11

Note:

  • We cannot use multiplication and division to have array elements addressed and their values.
  • We use %p to print the address.
  • *marks_array or marks_array[0] will fetch us the same values.
  • &marks_array and marks_array and &mark_array[0], will fetch us the same address using %p.
  • With the help of (*) we can dereference the pointer(address) to get the value at that memory location.
  • int *p = marks_array + 3; this is nothing but a pointer variable that holds the address of the 3rd element of the array that is the base address(marks_array) + 3).
  • Pointers are a variable that stores address.



Categories: C Language

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: