📖 Scenario: You are working on a simple program that manages a list of temperatures recorded over a week. You want to use pointers to access and print these temperatures from an array.
🎯 Goal: Build a C program that creates an array of temperatures, uses a pointer to access the array elements, and prints each temperature using the pointer.
📋 What You'll Learn
Create an integer array called temps with exactly 7 values: 23, 25, 22, 20, 24, 26, 21
Create a pointer variable called ptr that points to the first element of temps
Use a for loop with an integer variable i to iterate from 0 to 6
Inside the loop, use the pointer ptr and the index i to access and print each temperature
Print each temperature on its own line using printf
💡 Why This Matters
🌍 Real World
Pointers and arrays are used in many programs to efficiently access and manipulate lists of data, such as sensor readings or user inputs.
💼 Career
Understanding pointers and arrays is essential for C programming jobs, especially in systems programming, embedded development, and performance-critical applications.
Progress0 / 4 steps
1
Create the array of temperatures
Create an integer array called temps with these exact values: 23, 25, 22, 20, 24, 26, 21
C
Hint
Use the syntax int temps[7] = {value1, value2, ..., value7}; to create the array.
2
Create a pointer to the array
Create a pointer variable called ptr that points to the first element of the temps array
C
Hint
Set ptr equal to temps to point to the first element.
3
Use a for loop to access array elements via pointer
Use a for loop with an integer variable i from 0 to 6 to access each temperature using the pointer ptr and the index i
C
Hint
Use *(ptr + i) to get the value at index i via the pointer.
4
Print each temperature using the pointer
Inside the for loop, use printf to print each temperature accessed via *(ptr + i) on its own line
C
Hint
Use printf("%d\n", temp); to print each temperature on a new line.
Practice
(1/5)
1. What does a pointer to an array in C typically store?
easy
A. The address of the first element of the array
B. The total number of elements in the array
C. The sum of all elements in the array
D. The value of the last element in the array
Solution
Step 1: Understand pointer basics
A pointer stores a memory address, not values or counts.
Step 2: Pointer to array points to first element
In C, a pointer to an array points to the first element's address.
Final Answer:
The address of the first element of the array -> Option A
Quick Check:
Pointer = first element address [OK]
Hint: Pointers hold addresses, not values or counts [OK]
Common Mistakes:
Thinking pointer stores array size
Confusing pointer with array value
Assuming pointer stores last element
2. Which of the following is the correct way to declare a pointer to an integer array in C?
easy
A. int ptr[];
B. int *ptr;
C. int &ptr;
D. int ptr*;
Solution
Step 1: Recall pointer declaration syntax
In C, a pointer to int is declared as int *ptr;
Step 2: Check other options for syntax errors
int ptr[] declares an array, not a pointer; int &ptr is invalid in C; int ptr* is incorrect syntax.
Final Answer:
int *ptr; -> Option B
Quick Check:
Pointer syntax uses * before variable [OK]
Hint: Pointer declaration uses * before variable name [OK]
Common Mistakes:
Using array syntax instead of pointer
Confusing C++ reference (&) with pointer
Placing * after variable name incorrectly
3. What is the output of the following C code?
int arr[] = {10, 20, 30};
int *p = arr;
printf("%d", *(p + 1));
medium
A. 10
B. 30
C. Garbage value
D. 20
Solution
Step 1: Understand pointer arithmetic
p points to arr[0], so p + 1 points to arr[1].
Step 2: Dereference pointer at p + 1
*(p + 1) gives value at arr[1], which is 20.
Final Answer:
20 -> Option D
Quick Check:
*(p+1) = arr[1] = 20 [OK]
Hint: Pointer + 1 moves to next array element [OK]
Common Mistakes:
Confusing *(p+1) with *p
Assuming pointer arithmetic adds 1 byte
Expecting arr[0] value instead
4. Identify the error in this C code snippet:
int arr[3] = {1, 2, 3};
int *p = arr + 3;
printf("%d", *p);
medium
A. Missing semicolon after array declaration
B. Array size is incorrect
C. Pointer points outside array bounds causing undefined behavior
D. Pointer declaration syntax is wrong
Solution
Step 1: Analyze pointer assignment
arr + 3 points one element past the last array element (out of bounds).