---
title: "Pointers in pass by reference; 2D array"
subject: "Computer Programming"
chapter: 9
key: "semester_1/computer_programming:pointer-passing-by-reference-2d"
source: https://www.bctengineer.com/q/semester_1/computer_programming/pointer-passing-by-reference-2d
site: BCT Engineering
---

# Pointers in pass by reference; 2D array

Computer Programming · Chapter 9: Pointers

- Asked in: 2068 Shrawan, 4+4 marks
- Video: [Pointers and 2-D arrays](https://www.youtube.com/watch?v=sHcnvZA2u88) — mycodeschool
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/pointer-passing-by-reference-2d
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-9

## Question

What is the role of pointers in passing parameters to functions by reference? Give example, how 2 dimensional array can be accessed by a pointer.

## Short answer

Passing by reference in C uses pointers: pass the address of a variable, and the function dereferences it to modify the original. For a 2D array, a pointer to an array of columns (e.g., int (*p)[3]) can access elements as *(*(p+i)+j), or a flat pointer with row-major offset.

## Answer

#### Role of pointers in passing parameters by reference
C passes arguments by value, so a function normally changes only copies. If the **address** of a variable is passed to a function whose parameter is a pointer, the function can reach the original variable with `*` and change it. This is how call by reference is done in C. It also allows a function to return more than one value, and it avoids copying large arrays or structures (an array name is already an address).

```c
void swap(int *a, int *b) { int t = *a; *a = *b; *b = t; }
...
swap(&x, &y);       /* x and y are really exchanged */
```

#### Accessing a 2D array by a pointer
A 2D array is stored row by row. With `int m[2][3]`:
- `m` points to row 0; `m + i` points to row i.
- `*(m + i)` is row i (an array of 3 ints) and `*(*(m + i) + j)` is the element m[i][j].
- With a pointer to a row, `int (*p)[3] = m;`, the same expression `*(*(p + i) + j)` works.
- Or use a flat pointer `int *q = &m[0][0];` where element [i][j] is `*(q + i*3 + j)`.

```c
#include <stdio.h>

void swap(int *a, int *b)       /* pass by reference */
{
    int t = *a;
    *a = *b;
    *b = t;
}

int main(void)
{
    int x = 4, y = 9;
    int m[2][3] = {{1, 2, 3}, {4, 5, 6}};
    int (*p)[3] = m;                 /* pointer to an array of 3 ints */

    swap(&x, &y);
    printf("After swap: x = %d, y = %d\n", x, y);

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++)
            printf("%d ", *(*(p + i) + j));      /* same as m[i][j] */
        printf("\n");
    }
    int *q = &m[0][0];               /* flat access */
    printf("m[1][2] = %d = *(q+5) = %d\n", m[1][2], *(q + 5));
    return 0;
}
```

Output:

```text
After swap: x = 9, y = 4
1 2 3 
4 5 6 
m[1][2] = 6 = *(q+5) = 6
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Why can a function change the caller's variable when its address is passed?
   A. C passes arguments by reference by default
   B. The function reaches the original variable using * on the pointer parameter
   C. The compiler copies back the value
   D. Pointers are global
   Answer: B. C passes by value, but a passed address lets the function modify the original through *. (Key point: Role of pointers)

2. With int m[2][3], what is *(m + i)?
   A. The element m[i][0] value only
   B. Row i, an array of 3 ints
   C. The address of the whole array
   D. The element m[0][i]
   Answer: B. m + i points to row i, and dereferencing gives that row (an array of 3 ints). (Key point: Row of 2D array)

3. Which expression equals m[i][j]?
   A. *(m + i + j)
   B. *(*(m + i) + j)
   C. *(m + i) + j
   D. **(m + i + j)
   Answer: B. *(m + i) is row i, adding j moves to column j, and the outer * gives the element. (Key point: Element via pointer)

4. With int *q = &m[0][0]; and 3 columns, element [1][2] is:
   A. *(q + 3)
   B. *(q + 5)
   C. *(q + 6)
   D. *(q + 2)
   Answer: B. Row-wise storage gives offset i*3 + j = 1*3 + 2 = 5. (Key point: Flat pointer access)

## Related in Computer Programming

- [Pass 2D array; largest and smallest](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-2d-array-largest-smallest) — Computer Programming, chapter 7
- [Accessing array elements using pointer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-access-array-elements) — Computer Programming, chapter 9
- [Array of pointers](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aarray-of-pointers) — Computer Programming, chapter 9
- [Relation between array and pointer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aarray-pointer-relation) — Computer Programming, chapter 9, asked 5×
- [Reverse array and display via pointer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-reverse-array) — Computer Programming, chapter 9
- [Pass student array; display passed students](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-whole-array-student-info) — Computer Programming, chapter 7

## Related in other subjects

- [Namespace with pass by reference program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Anamespace-pass-by-ref-program) — Object Oriented Programming, chapter 2
- [Role of reference architectures](https://www.bctengineer.com/question.md?key=semester_5%2Fsoftware_engineering%3Areference-architecture) — Software Engineering, chapter 3
- [Linear shape functions for one-dimensional element](https://www.bctengineer.com/question.md?key=semester_8%2Ffinite_element_method%3Alinear-bar-shape-functions) — Finite Element Method, chapter 5
- [IANA, ICANN functions and IP address role](https://www.bctengineer.com/question.md?key=semester_8%2Finternet_and_intranet%3Aiana-icann-ip-role) — Internet and Intranet, chapter 1
