📘 Section 6 — Pointers
Understand memory addresses, pointer variables, and how to use pointers with arrays and functions.
🎯 What You’ll Learn
- What pointers are and why they matter
- How to declare and use pointer variables
- Pointer arithmetic with arrays
- Passing pointers to functions
1. Some Pointers Usage
- Dynamic memory allocation
- Passing parameters to functions
- Arrays and strings
- File handling
- Pointer Arithmetic and Complex Data Structures
2. What Is a Pointer?
A pointer is a variable that stores the memory address of another variable.
#include <stdio.h>
int main() {
int x = 10;
int *p = &x;
*p = 20; // changes x to 20 through the pointer
return 0;
}
How the Pointer Looks in Memory
After int *p = &x; and before *p = 20;, assume the computer stores x at address 1000, and stores the pointer p at address 2000.
The value inside p is 1000, so p stores the address of x.
So:
| Expression | Meaning | Value in this example |
|---|---|---|
x |
Value stored in x |
10 |
&x |
Address of x |
1000 |
p |
Value stored in pointer p |
1000 |
*p |
Go to address stored in p and get the value there |
10 |
&p |
Address of the pointer variable itself | 2000 |
A pointer is also a variable, so it has its own address. The special thing about a pointer is that the value inside it is an address.
When we write
*p = 20;, C goes to the address stored insidep(1000) and changes the value ofxfrom10to20.
Printing Pointer Info
int num = 42;
int *ptr = #
printf("Value of num: %d\n", num); // Output the value of 'num'
printf("Address of num: %p\n", &num); // Output the address of 'num'
printf("Value stored in ptr: %p\n", ptr); // Output the value stored in the pointer
printf("Value pointed to by ptr: %d\n", *ptr); // Output the value pointed to by the pointer
3. Pointers and Arrays
An array name acts as a pointer to its first element:
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr;
for (int i = 0; i < 5; i++) {
printf("%d ", *(ptr + i));
}
| Expression | Equivalent | Value |
|---|---|---|
*(ptr + 0) |
arr[0] |
1 |
*(ptr + 1) |
arr[1] |
2 |
*(ptr + 2) |
arr[2] |
3 |
💡
ptr + imoves the pointer forward byielements (notibytes — C handles the size automatically).
🧪 Exercises
Exercise 1: Print Array Using a Pointer
Task: Write a program in C to store n elements in an array and print the elements using a pointer.
💡 Hint
- Declare a pointer to the array:
int *ptr = arr; - Use
*ptrto access the current element, then move withptr++
🟢 Click to Show Solution
#include <stdio.h>
int main() {
int n;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int *ptr = arr;
printf("Array elements: ");
for (int i = 0; i < n; i++) {
printf("%d ", *ptr);
ptr++;
}
return 0;
}
Exercise 2: Sum & Average with Pointers
Task: Create a program that uses pointers to find the sum and average of elements in an array.
💡 Hint
- Traverse the array using pointer arithmetic
- Sum all elements, then divide by
nfor the average - Use
*ptrto read the current element, thenptr++to move to the next element - Use
floatordoublefor the average
🟢 Click to Show Solution
#include <stdio.h>
int main() {
int n;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int *ptr = arr;
int sum = 0;
for (int i = 0; i < n; i++) {
sum += *ptr;
ptr++;
}
printf("Sum = %d\n", sum);
printf("Average = %.2f\n", (float)sum / n);
return 0;
}
Exercise 3: Copy Array Using Pointers
Task: Write a program that uses pointers to copy one array to another.
💡 Hint
- Create two arrays and two pointers
- Loop through and assign
*ptr2 = *ptr1, then increment both pointers
🟢 Click to Show Solution
#include <stdio.h>
int main() {
int n;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr1[n], arr2[n];
printf("Enter %d elements: ", n);
int *ptr1 = arr1;
int *ptr2 = arr2;
for (int i = 0; i < n; i++) {
scanf("%d", ptr1);
ptr1++;
}
ptr1 = arr1;
for (int i = 0; i < n; i++) {
*ptr2 = *ptr1;
ptr1++;
ptr2++;
}
printf("Copied array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr2[i]);
}
return 0;
}
Exercise 4: Swap Two Numbers Using Pointers
Task: Write a function that takes two integer pointers and swaps the values of the numbers.
💡 Hint
- The function signature should be
void swap(int *a, int *b) - Use a temporary variable to hold one value during the swap
- Call with
swap(&x, &y)frommain
🟢 Click to Show Solution
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 5;
int y = 3;
printf("before swap: x= %d, y= %d\n", x, y);
swap(&x, &y);
printf("after swap: x= %d, y= %d\n", x, y);
return 0;
}
Output:
before swap: x= 5, y= 3
after swap: x= 3, y= 5
💡 This is call by reference — the function modifies the original variables because it has their addresses, not copies.
Exercise 5: Find Min & Max Using Pointers
Task: Write a function that takes an integer array and its size as parameters and updates two integer pointers to point to the maximum and minimum values in the array.
💡 Hint
- Function signature:
void maxAndMin(int arr[], int size, int *max, int *min) - Initialize
*maxand*minto the first element - Loop through and update using
*maxand*min
🟢 Click to Show Solution
#include <stdio.h>
void maxAndMin(int arr[], int size, int *max, int *min) {
*max = *min = arr[0];
for (int i = 1; i < size; i++) {
if (arr[i] > *max) {
*max = arr[i];
} else if (arr[i] < *min) {
*min = arr[i];
}
}
}
int main() {
int arr[] = {2, 1, 3, 4, 2, 2};
int size = sizeof(arr) / sizeof(arr[0]);
int max, min;
maxAndMin(arr, size, &max, &min);
printf("Max= %d - Min= %d\n", max, min);
return 0;
}
Output:
Max= 4 - Min= 1
💡 By passing
&maxand&min, the function can write results directly intomain’s variables — no return value needed for multiple outputs.
📋 Quick Reference
| Concept | Syntax |
|---|---|
| Declare pointer | int *ptr; |
| Get address | ptr = &variable; |
| Dereference (get value) | *ptr |
| Pointer arithmetic | *(ptr + i) is same as arr[i] |
| Pass by reference | void func(int *p) called with func(&x) |
⬅️ Back to Home