how to pass 2d array as a parameter

How to pass an array as a parameter in C?

In this blog post, you will learn how to pass arrays (both one-dimensional and multidimensional arrays) to a function in C programming with the help of examples programs. Mainly  I will focus on how to passing 2d array to the C function. But before explaining how to pass array in function, I want to give a quick introduction of the array.

An array is a collection of similar types of data, data could be value or address. When we pass an array as a parameter then it splits into the pointer to its first element. We can say that if I shall pass the array of characters as a parameter then it would be split into the pointer to the character. So, if a function parameter declared as T arr[] or T arr[n] is treated as T *arr.

In the C language, it is easy to work on the 1D array as compared to a multidimensional array. In this article, I will explain a few ways to pass the array as parameters. Here I will also explain the few methods to passing 2d array to function.

There are a lot of ways to pass the 1D array as arguments

Let’s see an example code, how to pass a 1D array as a parameter in the function. In the example program, I am creating an integer array and passing this integer array to the function as the parameter for tracing the array elements. The function parameter type could be as int aiData[] or int aiData[n] is treated as int *aiData.

1. Parameters as a pointer:

We know that array split into the pointer of its first element, so here I am creating a function whose parameters are an integer pointer.

#include <stdio.h>

//Size of the created array
#define ARRAY_SIZE  5


//Function to read array element
void ReadArray(int *paData)
{
    int index = 0;

    for(index= 0; index < ARRAY_SIZE; ++index)
    {
        printf("%d\n",paData[index]);

    }
}


int main(int argc, char *argv[])
{
    //Create an array
    int aiData[ARRAY_SIZE] = {1,2,3,4,5};


    //Pass array as a parameter
    ReadArray(aiData);

    return 0;
}

 

2. Parameters as a sized array:

One of the simple ways to pass the array as a parameter declared the function with prototype same as the array that will pass to the function.

#include <stdio.h>

//Size of the created array
#define ARRAY_SIZE  8

void ReadArray(int acData[ARRAY_SIZE])
{
    int index = 0;

    for(index= 0; index < ARRAY_SIZE; ++index)
    {
        printf("%d\n",acData[index]);

    }
}


int main(int argc, char *argv[])
{
//Create an array
    int aiData[ARRAY_SIZE] = {1,2,3,4,5,6,7,8};

    //Pass array as a parameter
    ReadArray(aiData);

    return 0;
}

 

3. Parameters as an unsized array:

When we pass the 1D array as a parameter then don’t need to specify the size of the array. It behaves like T *, where T is the type of the array.

#include <stdio.h>

//Size of the created array
#define ARRAY_SIZE  8

void ReadArray(int acData[])
{
    int index = 0;

    for(index= 0; index < ARRAY_SIZE; ++index)
    {
        printf("%d\n",acData[index]);

    }
}



int main(int argc, char *argv[])
{
    //Create an array
    int aiData[ARRAY_SIZE] = {1,2,3,4,5,6,7,8};

    //Pass array as a parameter
    ReadArray(aiData);

    return 0;
}

 




Ways to passing a 2D array as a parameter to the function

Similar to the 1D array we can pass the 2D array as a parameter to the function. It is important to remember that when we pass a 2D array as a parameter decays into a pointer to an array, not a pointer to a pointer.

1. Passing 2d array to function in C using pointers:

The first element of the multi-dimensional array is another array, so here, when we will pass a 2D array then it would be split into a pointer to the array.

For example,
If int aiData[3][3], is a 2D array of integers, it would be split into a pointer to the array of 3 integers (int (*)[3]).

#include <stdio.h>

//Size of the created array
#define ARRAY_ROW      3
#define ARRAY_COL      3


void ReadArray(int(*piData)[ARRAY_COL])
{
    int iRow = 0;
    int iCol = 0;

    for (iRow = 0; iRow < ARRAY_ROW; ++iRow)
    {
        for (iCol = 0; iCol < ARRAY_COL; ++iCol)
        {
            printf("%d\n", piData[iRow][iCol]);

        }
    }
}



int main(int argc, char *argv[])
{
    //Create an 2D array
    int aiData[ARRAY_ROW][ARRAY_COL] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

    //Pass array as a parameter
    ReadArray(aiData);

    return 0;
}

 

2. Passing 2d array to function with row and column:

Which prototype of the function should be the same as the passing array. In other words, we can say that if int aiData[3][3] is a 2D array, the function prototype should be similar to the 2D array.

#include <stdio.h>

//Size of the created array
#define ARRAY_ROW      3
#define ARRAY_COL      3


void ReadArray(int aiData[ARRAY_ROW][ARRAY_COL])
{
    int iRow = 0;
    int iCol = 0;

    for (iRow = 0; iRow < ARRAY_ROW; ++iRow)
    {
        for (iCol = 0; iCol < ARRAY_COL; ++iCol)
        {
            printf("%d\n", aiData[iRow][iCol]);

        }
    }

}



int main(int argc, char *argv[])
{
    //Create an 2D array
    int aiData[ARRAY_ROW][ARRAY_COL] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

    //Pass array as a parameter
    ReadArray(aiData);

    return 0;
}

 




3. Passing 2d array to function omitting the row:

In C language, the elements of the 2d array are stored row by row, there is no much more importance of the row when we are passing a 2d array to function. But it needs to remember that we have to specify the size of the column because it is used in jumping from row to row in memory.

#include <stdio.h>

//Size of the created array
#define ARRAY_ROW      3
#define ARRAY_COL      3


void ReadArray(int aiData[][ARRAY_COL])
{
    int iRow = 0;
    int iCol = 0;

    for (iRow = 0; iRow < ARRAY_ROW; ++iRow)
    {
        for (iCol = 0; iCol < ARRAY_COL; ++iCol)
        {
            printf("%d\n", aiData[iRow][iCol]);

        }
    }

}



int main(int argc, char *argv[])
{
    //Create an 2D array
    int aiData[ARRAY_ROW][ARRAY_COL] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

    //Pass array as a parameter
    ReadArray(aiData);

    return 0;
}

 

4. Passing 2d array to a function, using the pointer to a 2D array:

If int aiData[3][3] is a 2D array of integers, then &aiData would be pointer the 2d array that has 3 rows and 3 columns.

#include <stdio.h>

//Size of the created array
#define ARRAY_ROW      3
#define ARRAY_COL      3


void ReadArray(int(*piData)[ARRAY_ROW][ARRAY_COL])
{
    int iRow = 0;
    int iCol = 0;

    for (iRow = 0; iRow < ARRAY_ROW; ++iRow)
    {
        for (iCol = 0; iCol < ARRAY_COL; ++iCol)
        {
            printf("%d\n", (*piData)[iRow][iCol]);
        }
    }
}



int main(int argc, char *argv[])
{
    //Create an 2D array
    int aiData[ARRAY_ROW][ARRAY_COL] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

    //Pass array as a parameter
    ReadArray(&aiData);

    return 0;
}

 

Recommended Articles for you: