Arrays in C Programming

In this tutorial, we’ll learn about arrays in C Programming. Now the question is what are arrays and how are they useful? In all of our previous lessons we’ve used only few variables in each program. What if we need 100 variables of same data type? In this case we need to declare an array; arrays are group of item of same type under one variable name. It is an ordered list of elements where each is of the same data type. A one-dimensional array is represented by specifying the array name followed by a number in square brackets [ ] to indicate the array size or number of elements. Now let’s look at example code.

One Dimensional Array

Let’s declare a one dimensional array named myArray. This array contains four integer numbers. Now each block contains one integer number. In this example we have to access this value using address.

one dimensional array
One Dimensional Array
Example: 1D Arrays in C Programming
#include <stdio.h>

int main() {
   int myArray[4] = {11, 22, 33, 44};

   printf("\n myArray[0] ,value=%d : address=%u", myArray[0], &myArray[0]);
   printf("\n myArray[1] ,value=%d : address=%u", myArray[1], &myArray[1]);
   printf("\n myArray[2] ,value=%d : address=%u", myArray[2], &myArray[2]);
   printf("\n myArray[3] ,value=%d : address=%u", myArray[3], &myArray[3]);

   return (0);
}

Explanation: int myArray[4] this statement initializes an array i.e. myArray. This array is of type integer. The value between [] bracket represents the size of an array. In this case myArray[4] holds four integer numbers. The array organized as a block of memory. Each element in array stored in individual block as represented in picture. And each block has an address depending on your machine. The printf statements prints the value stored and address of each element.

1D Array in C Programming
1D Array in C Programming

This reveals that arrays are not only stored group of variables but also allows us to manipulate them. This make array as a powerful programming tool. Array shares very special relationship with pointer.learn-embedded-system

Array and Pointer Relationship

Array and pointer share very special relationship with each other. A pointer variable is used to store the address of another variable while an array is used to group related data items of same data type.

The name of the array is a pointer to the first element of the array. That means it holds the address of the very first element of the array. The example below will help you understand this relation.

Example: Array and Pointer Relationship
#include <stdio.h>

int main()
{
    int A[4]={11, 22, 33, 44};

    printf("%d \n", A);
    printf("%d \n", &A[0]);
    printf("%d \n", A[0]);
    printf("%d \n", *A);
}

Let’s build and run this example code and observe output. The output from this program is:

array and pointer relationship
Array and Pointer Relationship

Explanation: As per first printf statement, printing A will print the address of first element. Also printing &A[0] prints same address 2686736. This means when we print array usually it prints starting address of array. This is same as address of A[0]. When we print A[0], we will get the value of first element in array i.e. 11. As Array indexing starts with 0. Now when we print *A we will get same value 11 printed on screen. This means when we use pointer to an array name. This pointer points to starting address of an array.

As we know from our previous post pointers in C. We can use pointer arithmetic and loops to enhance use of Arrays in C Programming. We will dive deep in future lesson.

Multi-dimensional Arrays in C

The most basic form of multi-dimensional array is two dimensional arrays. The process of initialization of two dimensional arrays is same as one dimensional array. The only difference is we need two subscripts to declare two dimensional array. For example: int A[4][2]

Two Dimensional Arrays
Two Dimensional Arrays

Two dimensional (2D) array are generally known as matrix. The image above is an memory representation of 2D array A[4][2]. In this example ‘A’ is a name of an array, 4 and 2 are subscripts. Where 4 rows and 2 column uniquely identify each element in ‘A’. The example code below shows you how to declare 2D array. We will going to access individual element of an array.

#include <stdio.h>

int main()
{
   /* an array with 4 rows and 2 columns*/
   int A[4][2] = {{0,0}, {1,2}, {2,4}, {3,6}};
   int i, j;

   /* output each array element's value */
   for ( i = 0; i < 4; i++ ) {

      for ( j = 0; j < 2; j++ ) {
         printf("A[%d][%d] = %d\n", i,j, A[i][j] );
      }
   }

   return 0;
}

Let’s build and run this example code and observe output. The output from this program is:

Output of Two-dimensional Arrays
Output of Two-dimensional Arrays

As we have seen in this example. We can create arrays of any dimension. The most commonly used arrays are one or two dimensional. You may need further dimensional arrays in your program. This all depends on requirement and application itself.

Array as Function Argument in C

We can pass single dimensional array as an argument in a function. To do this we would have to declare formal parameter in one of following three ways and all three declaration methods produce similar results because each tells the compiler that an integer pointer is going to be received.

1st Way

int myFuncion(int myArray[], ...){
    .
    .
    .
}

2nd Way

int myFuncion(int myArray[SIZE], ...){
    .
    .
    .
}

3rd Way

int myFuncion(int* myArray, ...){
    .
    .
    .
} 
Example: Array as a Function Argument

Let’s take a look at example code. We have to pass an array ‘A’ to a function SumOfElements. It returns the sum of the numbers passed through the array, A[] = {1, 2, 3, 4, 5}.

#include <stdio.h>

int SumOfElements(int A[], int size)
{
    int i, sum = 0;

    for(i=0; i<size; i++)
    {
        sum += A[i];
    }

    return sum;
}

int main()
{
   int A[] = {1, 2, 3, 4, 5};
   int size = sizeof(A)/sizeof(A[0]);
   int total = SumOfElements(A, size);
   printf("Sum of elements = %d \n", total);
}

Now let’s run this code and We will get output shown below:

Output of Array as Function Argument
Output of Array as Function Argument

Pointer as a Function Argument

Example: Pointer as a Function
#include <stdio.h>

int SumOfElements(int *A, int size)
{
    int i, sum = 0;

    printf("SOE. size of A= %d, size of A[0]= %d \n", sizeof(A), sizeof(A[0]));

    for(i=0; i<size; i++)
    {
        sum += A[i];
    }

    return sum;
}

int main()
{
   int A[] = {1, 2, 3, 4, 5};
   int size = sizeof(A)/sizeof(A[0]);
   int total = SumOfElements(A, size);
   printf("Sum of elements = %d \n", total);
}

This is it for Arrays in C Programming. In next lesson we’ll learn about String in C Programming. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading.

Get Free Courses & Webinars
You'll receive only high quality learning material, tips & tricks
I agree to have my personal information transfered to MailChimp ( more information )
We respect your privacy

About Umesh Lokhande

Umesh Lokhande holds a Master degree in Scientific Instrumentation from University of Applied Sciences Jena, Germany. and has previously worked at Orbotech, Alere Technologies etc. Umesh is also a founder and first author of BINARYUPDATES.COM

Login

Register | Lost your password?