Home Data Structures Array in C/CPP Programming Langauages with Example

Array in C/CPP Programming Langauages with Example

An array in C or CPP is a collection of elements with similar data types stored in a contiguous memory location. The elements of an array can be accessed randomly using the index with time complexity of O(1). An array can store a collection of all primitives data types such as – int, float, char, double, etc.

Following is the pictorial representation of an array

 

Representation of an array in c/cpp
Representation of an array in c/cpp

The index of an array always starts at zero, and goto one less than the size of the array. For example, a 3 element array will have indices from zero to two.

Declaration of an Array in C/CPP

The declaration of an array is identical to the declaration of a variable except the variable name is followed by pair of square brackets [] for each dimension of the array. In this post, we will be discussing only 1D(one-dimensional array) so we will be using only one [].

The dimension of an array should always be a positive number.

Various ways to declare an Array:

1. Array declaration by specifying size
   int arr[5];

2. Array declaration by specifying size and initalizing elements
   int arr[5] = {1, 2, 3, 4, 5};

3. Array 
   int arr[5] = {};

4. Array declaration by initalizing elements
   int arr[] = {1, 2, 3, 4, 5};

5. Array declaration using pointers
   int *arr;

Accessing an element of an array

To access an element of an array, by using [] and providing the index of a particular element. For an array of size n, then the first element can be accessed using arr[0] and the last element can be accessed as arr[n-1].

int arr[5] = {1, 4, 9, 8, 5};
arr[0]    /* First element of the array, value = 1 */
arr[1]    /* Second element of the array, value = 4 */
arr[2]    /* Third element of the array, value = 9 */
arr[3]    /* Fourth element of the array, value = 8 */
arr[4]    /* Fifth(last) element of the array, value = 5 */

C program to find the sum of all the elements of an array

/* C program to find the
sum of all the elements
in an array */

#include <stdio.h>
int main()
{
    int i, sum = 0;

    //an array of size 6
    int arr[] = {5, 8, 4, 2, 11, 9};

    //calculating the size of an array
    int size = sizeof(arr)/sizeof(arr[0]);
    
    printf("Finding the location of each element of an array\n");
    printf("Size of integer in current compiler is = %d\n", sizeof(int));
    
    //the elements lies in the range 0-5
    for (i=0; i<6; i++){
        printf("Address of arr[%d] is %p \n", i, &arr[i]);
        sum = sum + arr[i];
    }
   
    printf("The sum of elements of the array is = %d \n", sum);

    return 0;
}
Output
Finding the memory location of each element of an array
Size of integer in current compiler is = 4
Address of arr[0] is 0x7fff2b40dcf0 
Address of arr[1] is 0x7fff2b40dcf4 
Address of arr[2] is 0x7fff2b40dcf8 
Address of arr[3] is 0x7fff2b40dcfc 
Address of arr[4] is 0x7fff2b40dd00 
Address of arr[5] is 0x7fff2b40dd04 
The sum of elements of the array is = 39 

As you can see from the above output the size of int is 4 bytes, the memory allocation to the array is done in the contiguous fashion, where each element occupies 4 bytes of memory. (The memory representation is in hexadecimal format)

Passing a 1D array to a function

When an array is passed to the function, it’s always passed by Reference. Hence we need to additionally pass the size of an array to the function.

There are two different ways to pass a 1D array to a function:

  1. Formal parameter as an unsized array
    void fun(int arr[], int size)
    {
    
    }
  2. Formal parameter as a pointer
    void fun(int *arr, int size)
    {
    
    }

Further Reading

  1. For an array with size n, the elements lie in the range 0 to n-1
  2. Call by Value and Call by Reference 
  3. A character array is always terminated by a Null character ‘/0’
  4. The size of an integer is 4 Byte and the size of a character is 1 Byte.
  5. The size of and array arr can be determined as int n = sizeof(arr)/sizeof(arr[0]);
  6. The elements are stored in unstored form and there can be repetitions too unlike sets.

Subscribe to our weekly newsletter

Join our community of 1000+ developers and stay updated with the fast moving world of computer science

We promise, we won't spam
Even we hate spam as much as you hate them

LEAVE A REPLY

Please enter your comment!
Please enter your name here