Arrays in C

Filed Under: C Programming

Why do we need Arrays?

Consider the problem of storing 10 integers. The naive way to store these integers would be to create 10 different integers variables and store 1 integer in each of them. This would be a viable solution but manipulating these variables would be a task in itself and we would have to keep track of their names always. Thus, not an efficient solution for a programmer.

So we introduce the concept of arrays. An array is a data structure that stores a fixed collection of variables of the same type. They are always present in the memory as contiguous blocks of data. Basically, you can create one variable who has different variables on its indexes. Indexes tell the memory location on which a variable is stored.

Arrays Diagram

How to Declare an Array in C?

The general structure of an array declaration is:


dataType arrayName[arraySize];

Note: The array size has to be an integer.

When an array is declared, depending on the compiler used, the initial values of the array may be 0s or garbage values as well. Thus, it is often good practice to initialize the array with some values.

Examples:


int arr[10] = {0}; //Simple Declaration with all elements 0
double price[5]; // Any data type allowed, including structs
long int phoneNumber[] = {100, 101, 102}; //Implicit size declaration

How to Access Array Elements?

There are two ways to access an element in an array in C.

  1. Using square brackets.
  2. Using pointers.

Note: In some languages, the indexing is 0-based (C/C++/Python/Java) whereas it is 1-based in others (MATLAB/Octave). In 0-based languages, the first element is present at the 0th index, and in 1-based languages, the first element is present at the 1st index.

1. Accessing Array Elements Using Square Brackets


arr[5];

Here we write the name of the variable followed by the square brackets that contain the index.

2. Accessing Array Element Using Pointer


*(arr + 5);

Arrays are present in the memory as contiguous blocks of data and thus, it possible to access the memory of the elements directly using pointers.

Initially, the variable itself refers to the first element of the array, so if we write *arr we can get the value of the first element.

If we want the ith element, then we can get the value by moving “i” memory blocks ahead in the memory, from the point where the variable is present.

Note: At compile time, array[idx] is converted to *(array + x) for execution. This brings up an interesting idea. We know that addition is commutative, so this would mean that idx[arr] should also be a valid way to write, and in fact, this is true. Writing idx[arr] will give the same output as arr[idx].

Arrays Example in C


#include<stdio.h>

#define N 5

int main(){

    /*
    Note: Here we can use arr[i] and *(arr + i) both according
    to our need and preference.
    */

    int arr[N]; //Simple definition of array
    int i;

    for(i = 0; i < N; i++){
        printf("Enter element %d : ", i);
        //Read elements at the memory from user
        scanf("%d", &arr[i]);
    }

    for(i = 0; i < N; i++){
        printf("Element at position %d is : %d\n", i, arr[i]);
    }
}

arrays example in C

Conclusion

On a concluding note, it can be ascertained that arrays are a simple and efficient way to store, organize and manipulate data of the same type through indexing without using repetitive variable names.

We have also observed that arrays are present in the memory as contiguous blocks of data and thus, the data can be extracted from the indices using pointers as well.

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages