How to access two dimensional array using pointers in C

How to access two dimensional array using pointers in C

I have written a lot of articles on array and pointer if you want you can see this link, C Tutorial. Nowadays many students ask me a question that how to access a multidimensional array with a pointer in C or access two dimensional array using pointers in C I have replied many students but every month I found this question in my Inbox.

So I have decided to write an article on how to access a multidimensional array with a pointer (Access two-dimensional array using pointers in C). I am assuming you are already familiar with a multidimensional array, if you don’t have the knowledge of array, then you should check this article, brief introduction of an array.

 

Relationship between array and pointer

In C-language pointer and array are very close to each other, an array can be split in the form of the pointer. The name of the array is a pointer to its first element. So if acData is an array of character then acData will be the address of its first element. You can also say that acData is similar to the &acData[0]

Below expression describe a relation between array and pointer,

acData [i] = *(acData +i) ————————->1D array in form of pointer

a[i] = *(a+ i) ————————->ith element of an 1D array

acData [i][j] = *(acData [i]+j); ————————–>2D array in form of 1D array and pointer.

acData [i][j] = *(*(acData + i) + j) ———————->2D array in form of pointer.

 

Note Array elements stored in a consecutive memory block, so we can access the elements of the array using the pointer.

 

Access a 2d array using a single pointer

In C language, the compiler calculates offset to access the element of the array. The calculation of the offset depends on the array dimensions.

Let’s take an example,

Suppose int aiData[3][3] is a 2D array that has 3 rows and 3 columns.  If you need to access the 2nd element of 1 row in aiData, then calculates its offset that will be (1 * coloumb_number) + 2 ).  Now to access the element just add the offset in array base address and dereference it.

Note: Array index always starts with 0, so 2nd means third element.

 

See the below steps for the above description,

calculate offset => offset = (1 * coloumb_number)+ 2);

Add offset in array base address => (int *)aiData + offset; //here

typecast with int pointer because aiData is an array of integer

Get the element => *( (int *)aiData + offset );

 

Note: General expression to calculates offset for 2D array is that, (ithRow * Total_number_Coloumb)+ jthColoumb).

 

#include <stdio.h>

#define ROW     3
#define COL     3

int main(void)
{
    // 2d array
    int aiData [ROW][COL] = { { 9, 6, 1 }, { 144, 70, 50 }, {10, 12, 78} };

    int *piData = NULL; //pointer to integer

    int iRow =0, iCol =0;


    piData = &aiData[0][0]; //You can also write *aiData

    for (iRow = 0; iRow < ROW; ++iRow) //Loop of row
    {
        for (iCol = 0; iCol < COL; ++iCol)// Loop for coloum
        {
            //Read element of 2D array
            printf("aiData[%d][%d] = %d\n",iRow,iCol, *(piData + ( iRow * COL) + iCol));
        }
    }

    return 0;
}

 

 

We know that the array element is stored in the contiguous form so we can also access the elements of the two-dimensional array to calculate the total number of cells.

See the below program,

#include <stdio.h>

#define ROW     3 // number of rows in array

#define COL     3 // number of col in array

#define TOTAL_CELLS (ROW * COL) //totall cells in array


int main(void)
{
    // 2d array
    int aiData [ROW][COL] = { { 9, 6, 1 }, { 144, 70, 50 }, {10, 12, 78} };

    int *piData = NULL; //pointer to integer

    int arrayIndex = 0; //variable for array index

    piData = &aiData[0][0]; //You can also write *aiData

    for (arrayIndex = 0; arrayIndex < TOTAL_CELLS; ++arrayIndex) //Loop of row
    {
        printf(" array elements = %d\n", *(piData + arrayIndex ));

    }

    return 0;
}

 

 

If you want to learn more about the c language, here 10 Free days (up to 200 minutes) C video course for you.

C tutorial

Access 2d array using a pointer to an array

We can easily access a 2D array with the help of a pointer to the array. First, we need to define a new type for the 2d array using the typedef that helps you to avoid the complex syntax. If you don’t know typedef see this article, application of typedef. After the creation of a new type for the 2d array, create a pointer to the 2d array and assign the address of the 2d array to the pointer.

#include <stdio.h>


#define ROW 	3
#define COL 	3

typedef int Array2D[ROW][COL]; //New type

int main(void)
{
    // 2d array
    Array2D aiData = { { 9, 6, 1 }, { 144, 70, 50 }, {10, 12, 78} };

    Array2D *p2DArray = NULL; //Pointer to the 2d Array

    int iRow =0, iCol =0; //Row and col


    p2DArray = &aiData; //Assign address of array to the pointer

    for (iRow = 0; iRow < ROW; ++iRow) //Loop of row
    {
        for (iCol = 0; iCol < COL; ++iCol)// Loop for coloumb
        {
            //Read element of 2D array
            printf("aiData[%d][%d] = %d\n",iRow,iCol, (*p2DArray)[iRow][iCol]);
        }
    }

    return 0;
}

 

Similar to the two-dimensional array we can access three, fourth, … etc dimensional array using the pointers.

 

Recommended Articles for you:

 



31 comments

  1. #include
    #include
    void main()
    {
    int a[2][2],i,j,*p;
    printf(“enter the value of rows and column\n”);
    for(i=0;i<=1;i++)
    {
    for(j=0;j<=1;j++)
    {
    scanf("%d",&a[i][j]);
    }
    }
    printf("\n");
    p=&a[0][0];
    for(i=0;i<=1;i++)
    {
    for(j=0;j<=1;j++)
    {

    printf("%d ",*(p+i*2+j)); //if it is 3*3 matrix then we use *(p+1*3+j) and so on;
    }
    printf("\n");
    }
    getche();

    }

Leave a Reply

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