MCS-201 · Unit 7

Pointers

Section 5 of 7

Block 2 · Unit 7Pointers12 min · Section 5 of 7

7.5Arrays and Pointers

Covers IGNOU MCS-201 §7.7

Arrays and pointers are closely linked in C: every subscript you have written so far was really a pointer operation. Understanding this link explains why arrays are passed to functions by address and makes pointer notation for arrays easy to read.

An array name as an address

In most expressions, the name of an array is automatically converted to a pointer to its first element. So for int arr[5];, the expression arr has the same value as &arr[0].

Subscript notationPointer notationMeaning
&arr[0]arrAddress of the first element
&arr[i]arr + iAddress of element i
arr[0]*arrValue of the first element
arr[i]*(arr + i)Value of element i

The same holds for a pointer: after int *p = arr;, p[i] and *(p + i) both mean arr[i].

An array is not a pointer

Array int arr[5];Pointer int *p = arr;
What it isFive int objectsOne variable holding an address
sizeof20 (5 × 4 bytes)8
Can be made to refer elsewhereNo: arr = ... and arr++ are errorsYes: p = ..., p++
Elements can be assignedYes: arr[1] = 3;Yes: p[1] = 3;

Pointers and two-dimensional arrays

A two-dimensional array is an array of rows. For int arr[3][3];:

ExpressionMeaning
arrPointer to row 0
arr + iPointer to row i
*(arr + i) or arr[i]Row i itself, which converts to a pointer to its first element
*(arr + i) + jAddress of element j in row i
*(*(arr + i) + j)Value of arr[i][j]

Read *(*(arr + i) + j) from the inside out: go to row i, step to column j, take the value.

Key takeaways

  • In expressions, an array name becomes a pointer to its first element: arr equals &arr[0].
  • arr[i] is *(arr + i); &arr[i] is arr + i.
  • Arrays are not pointers: different sizeof, and an array name cannot be assigned.
  • arr[i][j] is *(*(arr + i) + j).

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

For int a[4] = {3, 6, 9, 12};, what is *(a + 2)?

Show answer

Answer: C. 9

*(a + 2) is a[2], which is 9. The value 5 comes from wrongly computing *a + 2.

Question 2 · multiple choice

Which statement is NOT valid for int arr[5]; and int *p;?

Show answer

Answer: D. arr++;

An array name cannot be modified. A pointer variable can be incremented, and array elements can be assigned.

Question 3 · multiple choice

On a 64-bit system with 4-byte int, what are sizeof arr and sizeof p for int arr[10]; int *p = arr;?

Show answer

Answer: B. 40 and 8

sizeof arr is the whole array, 10 × 4 = 40 bytes; sizeof p is the size of one pointer, 8 bytes.

Question 4 · multiple choice

For int m[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};, what is *(*(m + 1) + 2)?

Show answer

Answer: D. 6

Row 1 is {4, 5, 6} and column 2 of that row is 6. It is the same as m[1][2].

Question 5 · multiple choice

What does this program print?

C · star_arr.c
#include <stdio.h>

int main(void)
{
    int a[3] = {10, 20, 30};
    printf("%d %d\n", *a + 1, *(a + 1));
    return 0;
}
Show answer

Answer: C. 11 20

*a + 1 is the first element plus 1, that is 11; *(a + 1) is the second element, 20. The parentheses decide which is meant.

Question 6 · short answer

Write a function sum(int *p, int n) that adds the elements of an array using pointer notation only (no square brackets), and show how it is called for int a[4] = {1, 2, 3, 4};.

Show worked answer

int sum(int *p, int n) { int i, total = 0; for (i = 0; i < n; i++) total = total + *(p + i); return total; }. Call: printf("%d\n", sum(a, 4));. The array name a is converted to the address of its first element, which is copied into p; *(p + i) is then the i-th element, so the function returns 1 + 2 + 3 + 4 = 10.

↑ ↓ move↵ openTab switch type