In the previous section we discussed the basics of C Pointers. Now let us take a look at some real-life application Problems.
1. Student Marks Management System (Using Pointers)
Store marks of N students, calculate average, highest and lowest marks using pointers
#include
int main() {
int n, i;
float marks[50], sum = 0;
float *p = marks;
printf("Enter number of students: ");
scanf("%d", &n);
for(i = 0; i < n; i++) {
printf("Enter marks of student %d: ", i+1);
scanf("%f", (p + i));
sum += *(p + i);
}
float max = *p, min = *p;
for(i = 1; i < n; i++) {
if(*(p + i) > max)
max = *(p + i);
if(*(p + i) < min)
min = *(p + i);
}
printf("\nAverage = %.2f", sum / n);
printf("\nHighest = %.2f", max);
printf("\nLowest = %.2f", min);
return 0;
}
// Enter number of students: 4
// Enter marks of student 1: 50
// Enter marks of student 2: 35
// Enter marks of student 3: 28
// Enter marks of student 4: 47
// Average = 40.00
// Highest = 50.00
// Lowest = 28.00
| Step (i) | Pointer Address Calculation | Physical Address | Value *(p+i) | Accumulation (Sum) | Max Logic | Min Logic | Updated Min/Max |
|---|---|---|---|---|---|---|---|
| 0 | 1000 + (0 * 4) | 1000 | 50.0 | 0 + 50 = 50 | - | - | Max: 50, Min: 50 |
| 1 | 1000 + (1 * 4) | 1004 | 35.0 | 50 + 35 = 85 | 35 > 50 (F) | 35 < 50 (T) | Max: 50, Min: 35 |
| 2 | 1000 + (2 * 4) | 1008 | 28.0 | 85 + 28 = 113 | 28 > 50 (F) | 28 < 35 (T) | Max: 50, Min: 28 |
| 3 | 1000 + (3 * 4) | 1012 | 47.0 | 113 + 47 = 160 | 47 > 50 (F) | 47 < 28 (F) | Max: 50, Min: 28 |
2. Swapping Two Numbers (Call by Reference)
Swap two numbers without returning values, using pointers.
#include
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x, y;
printf("Enter two numbers: ");
scanf("%d %d", &x, &y);
swap(&x, &y);
printf("After swapping: x = %d, y = %d", x, y);
return 0;
}
// Enter two numbers: 4 6
// After swapping: x = 6, y = 4
3. String Length Without Using strlen()
Find the length of a string using pointers.
#include
int main() {
char str[100];
char *p;
int count = 0;
printf("Enter a string: ");
scanf("%s", str);
p = str;
while(*p != '\0') {
count++;
p++;
}
printf("Length of string = %d", count);
return 0;
}
// Enter a string: ALEX
// Length of string = 4
| Step | Pointer Address Calculation | Character *p | Condition *p != '\0' | Count Update |
|---|---|---|---|---|
| 1 | 2000 + (0 * 1) | 'A' | True | 0 + 1 = 1 |
| 2 | 2000 + (1 * 1) | 'L' | True | 1 + 1 = 2 |
| 3 | 2000 + (2 * 1) | 'E' | True | 2 + 1 = 3 |
| 4 | 2000 + (3 * 1) | 'X' | True | 3 + 1 = 4 |
| 5 | 2000 + (4 * 1) | '\0' | False (Stop) | - |
4. Finding the Largest Element Using a Pointer Function
Find the largest element in an array using a function and pointers.
#include
int findMax(int *arr, int n) {
int max = arr[0];
for(int i = 1; i < n; i++) {
if(*(arr + i) > max)
max = *(arr + i);
}
return max;
}
int main() {
int n, arr[50];
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter the Elements: ");
for(int i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("Largest element = %d", findMax(arr, n));
return 0;
}
// Enter number of elements: 5
// Enter the Elements: 8 19 4 6 3
// Largest element = 19
| Step (i) | Pointer Expression | Value *(arr + i) | Max Logic | Updated Max |
|---|---|---|---|---|
| Initial | arr[0] | 8 | (First element) | 8 |
| 1 | arr + 1 | 19 | 19 > 8 (True) | 19 |
| 2 | arr + 2 | 4 | 4 > 19 (False) | 19 |
| 3 | arr + 3 | 6 | 6 > 19 (False) | 19 |
| 4 | arr + 4 | 3 | 3 > 19 (False) | 19 |
5. Reverse an Array Using Pointers
Reverse the elements of an array without using another array.
#include
int main() {
int n, i, temp;
int arr[50];
int *p = arr;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter the Elements: ");
for(i = 0; i < n; i++)
scanf("%d", (p + i));
for(i = 0; i < n / 2; i++) {
temp = *(p + i);
*(p + i) = *(p + n - i - 1);
*(p + n - i - 1) = temp;
}
printf("Reversed array:\n");
for(i = 0; i < n; i++)
printf("%d ", *(p + i));
return 0;
}
// Enter number of elements: 5
// Enter the Elements: 4 0 3 9 6
// Reversed array:
// 6 9 3 0 4