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.
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
- Read and validate the array size.
- Set p to the beginning of the array.
- Read each value into the address p + i.
- Validate each stored integer.
- 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.
On a phone, scroll sideways to read the diagram at full size. Open full-size flowchart ↗
#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;
}
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
./labOn Windows, run .\lab.exe after compiling with GCC. The interest program requires the math library where applicable.
FOLLOW THE VALUES
Dry run
| Step / state | Operation | Result |
|---|---|---|
| p = a | p + 0 addresses a[0] | Store 10 |
| p + 1 | Addresses a[1] | Store 20 |
| p + 2 | Addresses a[2] | Store 30 |
| Display a[0], a[1], a[2] | Array indexing reads the same storage | 10 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
3
10 20 30
Array: 10 20 30
Sample 2
1
-4
Array: -4
Sample 3
4
0 0 -1 2
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.