C Pointers Demystified: Memory Addresses, Operators and Examples

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 <stdio.h>
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 CalculationPhysical AddressValue *(p+i)Accumulation (Sum)Max LogicMin LogicUpdated Min/Max
01000 + (0 * 4)100050.00 + 50 = 50--Max: 50, Min: 50
11000 + (1 * 4)100435.050 + 35 = 8535 > 50 (F)35 < 50 (T)Max: 50, Min: 35
21000 + (2 * 4)100828.085 + 28 = 11328 > 50 (F)28 < 35 (T)Max: 50, Min: 28
31000 + (3 * 4)101247.0113 + 47 = 16047 > 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 <stdio.h>
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 <stdio.h>
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

				
			
StepPointer Address CalculationCharacter *pCondition *p != '\0'Count Update
12000 + (0 * 1)'A'True0 + 1 = 1
22000 + (1 * 1)'L'True1 + 1 = 2
32000 + (2 * 1)'E'True2 + 1 = 3
42000 + (3 * 1)'X'True3 + 1 = 4
52000 + (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 <stdio.h>
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 ExpressionValue *(arr + i)Max LogicUpdated Max
Initialarr[0]8(First element)8
1arr + 11919 > 8 (True)19
2arr + 244 > 19 (False)19
3arr + 366 > 19 (False)19
4arr + 433 > 19 (False)19

5. Reverse an Array Using Pointers

Reverse the elements of an array without using another array.

				
					#include <stdio.h>
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