Skip to lab content

UNIT 03 · Arrays, Pointers and Functions

Read through a pointer; display through an array

EXERCISE 03CC173 sample runs

THE SYLLABUS QUESTION

What you need to solve

Write a program for reading elements using a pointer into an array and display the values using the array.
Input format & conventions

Array size n (1–100), followed by n integers between -1,000,000 and 1,000,000.

UNDERSTAND THE IDEA

Explanation

When an array is used in this expression, its name points to the first element. Assign int *p = a to access the same storage through a pointer.

scanf needs an address. p + i is the address of a[i], so scanf("%d", p + i) writes directly into the array. Displaying a[i] then reads the stored value normally.

PLAN BEFORE CODING

Algorithm

  1. Read and validate the array size.
  2. Set p to the beginning of the array.
  3. Read each value into the address p + i.
  4. Validate each stored integer.
  5. Display the values using array indexing a[i].

SEE THE CONTROL FLOW

Flowchart

Follow the arrows from Start. Diamonds ask a question; labeled arrows show the answer. A returning arrow repeats a loop. Function internals are grouped where needed; later input checks follow the rules in the program.

Flowchart for Read through a pointer; display through an array: input, decisions, processing, output and loop paths

On a phone, scroll sideways to read the diagram at full size. Open full-size flowchart ↗

C17

Complete C program

Download .c
#include <stdio.h>
#define MAX_SIZE 100

int main(void) {
    int a[MAX_SIZE], n;
    int *p = a;
    if (scanf("%d", &n) != 1 || n < 1 || n > MAX_SIZE) {
        puts("Invalid input."); return 1;
    }
    for (int i = 0; i < n; ++i) {
        if (scanf("%d", p + i) != 1 || *(p + i) < -1000000 || *(p + i) > 1000000) {
            puts("Invalid input."); return 1;
        }
    }
    printf("Array: ");
    for (int i = 0; i < n; ++i) printf("%s%d", i == 0 ? "" : " ", a[i]);
    putchar('\n');
    return 0;
}
Open in compiler ↗

Code loads into the existing compiler. Enter the sample input there; sign-in and execution rules stay the same.

Compile and run locally
gcc -std=c17 pointer-array-input.c -o lab
./lab

On Windows, run .\lab.exe after compiling with GCC. The interest program requires the math library where applicable.

FOLLOW THE VALUES

Dry run

Step / stateOperationResult
p = ap + 0 addresses a[0]Store 10
p + 1Addresses a[1]Store 20
p + 2Addresses a[2]Store 30
Display a[0], a[1], a[2]Array indexing reads the same storage10 20 30

CHECK THE BEHAVIOR

Sample input & output

Each output below was produced by compiling and running this exact program. Input values are entered in the stated order; the examples do not print input prompts.

Sample 1

INPUT
3
10 20 30
OUTPUT
Array: 10 20 30

Sample 2

INPUT
1
-4
OUTPUT
Array: -4

Sample 3

INPUT
4
0 0 -1 2
OUTPUT
Array: 0 0 -1 2

Common mistakes

  • scanf("%d", *(p + i)) passes a value, not an address.
  • Keep pointer arithmetic inside the allocated array.

WHY THIS GROWTH RATE?

Time and space complexity

Time O(n); O(n) array storage and O(1) additional working space.

For an array of n values, the input loop visits n addresses through p + i. The display loop then prints n elements through a[i]. Pointer arithmetic does not eliminate either traversal.

Two consecutive linear loops give c1 × n + c2 × n work, which simplifies to O(n) time. The array uses O(n) storage; the pointer and counters add O(1) working space.

Big-O describes how work grows as the stated input quantity grows; fixed factors and lower-order terms are omitted. The analysis treats fixed-width arithmetic as constant cost and the published limits as practical safety bounds.