Skip to content
Laviyx Learn

Try

PPS Lab · Experiment 25

Read array elements using a pointer

CArrays, pointers and functionsJNTUH R22 · CS108ES

Aim

To write a C program that reads n integers into an array through a pointer and then displays the values using ordinary array indexing.

Algorithm

  1. Declare an integer array a and an integer pointer p.
  2. Read n. If it is not between 1 and 50, print an error and stop.
  3. Set p to point to the first element of the array: p = a.
  4. For i from 0 to n − 1, read a value into the address p + i.
  5. For i from 0 to n − 1, print a[i] using the array name and index.

Program

C
#include <stdio.h>

#define MAX 50

int main(void) {
    int a[MAX];
    int *p = a;   /* p points to a[0] */
    int n;

    printf("Enter the number of elements (1 to %d): ", MAX);
    if (scanf("%d", &n) != 1 || n < 1 || n > MAX) {
        printf("Invalid number of elements\n");
        return 1;
    }

    printf("Enter %d integers: ", n);
    for (int i = 0; i < n; i++) {
        /* p + i is the address of a[i], so no & is needed */
        if (scanf("%d", p + i) != 1) {
            printf("Invalid input\n");
            return 1;
        }
    }

    printf("Array elements (displayed using the array):\n");
    for (int i = 0; i < n; i++) {
        printf("a[%d] = %d\n", i, a[i]);
    }
    return 0;
}

Output

Output
Enter the number of elements (1 to 50): 5
Enter 5 integers: 10 20 30 40 50
Array elements (displayed using the array):
a[0] = 10
a[1] = 20
a[2] = 30
a[3] = 40
a[4] = 50

Recorded by compiling and running this exact program. Typed input appears after each prompt.

How it works

The name of an array, used in an expression, gives the address of its first element, so int *p = a; makes p point to a[0].

Adding i to a pointer moves it forward by i elements, not i bytes, so p + i is the address of a[i] (the same as &a[i]). That is why scanf gets p + i with no &.

The values were stored through p, but printing a[i] shows them, because p and a refer to the same memory.

Common mistakes

  • Writing scanf("%d", &(p + i)) or scanf("%d", *(p + i)). p + i is already an address. & of it is a compile error, and *(p + i) passes the value, not the address.

  • Declaring int *p; without setting it to a. An uninitialised pointer points to an unknown place, and writing through it can crash the program.

  • Writing scanf("%d", p++) and then printing through p later. After the loop p points past the last element, so it must be reset to a before it is used again.

  • Thinking p + 1 moves one byte. It moves one int (usually 4 bytes), because pointer arithmetic is done in units of the pointed-to type.

Viva questions

Try to answer each one aloud before you open it.

  • What is a pointer?

    A variable that stores the address of another variable. int *p declares p as a pointer to an int.

  • What is the relation between an array name and a pointer?

    In an expression, the array name gives the address of its first element. So a[i] is the same as *(a + i), and &a[i] is the same as a + i.

  • If p points to a[0], what are p + 2 and *(p + 2)?

    p + 2 is the address of a[2], and *(p + 2) is the value stored in a[2].

  • What is the difference between the & and * operators?

    & gives the address of a variable. * (dereference) gives the value stored at an address.

  • Can you write a = a + 1 for an array a?

    No. An array name is not a variable that can be changed; it always refers to the same memory. A pointer variable like p can be changed with p = p + 1.

Learn the concepts

All Programming for Problem Solving Lab experiments →