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. I have replied many students but every week 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. 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:  An 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, 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 start with 0, so 2nd means third element.

See the below steps for 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 );

See: Pointer arithmatic in C.

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

 

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

See the below program,

 

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 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 new type for the 2d array, create a pointer to the 2d array and assign the address of the 2d array to the pointer.

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

You can also see: How to pass an array as a parameter in C?



2 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