All Exams Test series for 1 year @ ₹349 only
Question

What would be the equivalent pointer expression for referring the array element ar[m][n][o]

The correct answer is
*(*(*(ar + m) + n) + o)

Pointer Expression for Array Element ar[m][n][o]

Understanding how multi-dimensional arrays are represented in memory using pointers is crucial in C and C++. Let's break down how to find the pointer expression equivalent to accessing the element $ar[m][m][o]$.

Understanding Multi-dimensional Arrays and Pointers

In C/C++, a multi-dimensional array like $ar[m][n][o]$ is essentially an array of arrays. A 3D array can be thought of as a 2D array where each element is a 1D array.

  • The name of the array, $ar$, when used in an expression, typically decays into a pointer to its first element. For a 3D array $ar[X][Y][Z]$, $ar$ decays into a pointer to its first element, which is a 2D array of type $int[Y][Z]$. So, $ar$ points to the start of the first 2D slice. Its type is effectively $int (*)[Y][Z]$.
  • Pointer arithmetic is used to navigate through the memory blocks allocated for the array. Adding an integer $k$ to a pointer moves the pointer forward by $k$ times the size of the data type it points to.

Deriving the Pointer Expression

Let's derive the expression step by step, starting from the base array name $ar$:

  1. Accessing the 'm'-th 2D slice:
    • The expression $ar + m$ calculates the memory address of the start of the $m$-th 2D slice (where the first slice is 0).
    • Dereferencing this gives the actual 2D array: $*(ar + m)$. This expression is equivalent to $ar[m]$. This resulting pointer points to the first row (a 1D array) of the $m$-th 2D slice. Its type is effectively $int (*)[Z]$.
  2. Accessing the 'n'-th row within the 'm'-th slice:
    • Taking the pointer from the previous step, $*(ar + m)$, and adding $n$ gives: $*(ar + m) + n$. This points to the start of the $n$-th row within the $m$-th 2D slice.
    • Dereferencing this yields the actual 1D array (row): $*(*(ar + m) + n)$. This expression is equivalent to $ar[m][n]$. This resulting pointer points to the first integer element of that row. Its type is effectively $int *$.
  3. Accessing the 'o'-th element within the 'n'-th row:
    • Taking the pointer from the previous step, $*(*(ar + m) + n)$, and adding $o$ gives: $*(*(ar + m) + n) + o$. This points precisely to the memory location of the desired element.
    • Finally, dereferencing this pointer gives the value of the element: $*(*(*(ar + m) + n) + o)$. This is the pointer expression equivalent to $ar[m][n][o]$.

Summary of Equivalencies

The core idea is that array indexing $a[i]$ is equivalent to pointer dereferencing $*(a + i)$. Applying this repeatedly for a 3D array leads to the final expression:

$ar[m][n][o]$ is equivalent to $*(*(*(ar + m) + n) + o)$.

This expression correctly navigates the memory structure of the 3D array using pointer arithmetic to access the specific element $ar[m][n][o]$.

Was this answer helpful?

Important Questions from Pointer

  1. Only legal pointer operations:
    A. pointer + number $\rightarrow$ pointer
    B. pointer - number $\rightarrow$ number
    C. pointer + pointer $\rightarrow$ pointer
    D. pointer - pointer $\rightarrow$ pointer
    E. pointer - pointer $\rightarrow$ number
    Choose the most appropriate answer from the options given below:
  2. Consider the following C program:
    #include <stdio.h>
    void stringcopy(char *, char *);
    int main(){
    char a[30] = "@#Hello World!";
    stringcopy(a, a + 2);
    printf("%s\n", a);
    return 0;
    }
    void stringcopy(char *s, char *t) {
    while(*t)
    *s++ = *t++;
    }
    Which ONE of the following will be the output of the program?

  3. Consider the following C program:
    #include <stdio.h>
    int main(){
    int a;
    int arr[5] = {30,50,10};
    int *ptr;
    ptr = & arr[0] + 1;
    a = *ptr;
    (*ptr)++;
    ptr++;
    printf("%d", a + (*ptr) + arr[1]);
    return 0;
    }
    The output of the above program is ____________ (Answer in integer)

  4. Consider the following ANSI-C program.

    #include <stdio.h>
    int main(){
     int *ptr, a, b, c;
     a=5; b=11; c=20;
     ptr=&a; *ptr=c; ptr=&c;
     a=*(&b); c=*ptr-a;
     printf("%d",c);
     return(0);
    }

    The output of this program is ____________. (answer in integer)

    Note: Assume that the program compiles and runs successfully.

  5. The following statement in ‘C’
    int (*f())[ ];
    declares
Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App