Solutions to C Pointer Practice Questions

These worked examples correspond to the pointer exercises on the Pointers in C tutorial. Each solution is complete and can be compiled independently.

1. Display Addresses of Variables Top ↑

#include <stdio.h>

int main(void) {
    int number = 10;
    float value = 2.5f;

    printf("int address   : %p\n", (void *)&number);
    printf("float address : %p\n", (void *)&value);

    return 0;
}

2. Store the Address of a long Variable Top ↑

#include <stdio.h>

int main(void) {
    long value = 2147483647L;
    long *ptr = &value;

    printf("value = %ld\n", *ptr);
    printf("address = %p\n", (void *)ptr);

    return 0;
}

3. Store a String Address in a Pointer Top ↑

#include <stdio.h>

int main(void) {
    char text[] = "plus2net";
    char *ptr = text;

    while (*ptr != '\0') {
        printf("address = %p, value = %c\n",
               (void *)ptr, *ptr);
        ptr++;
    }

    return 0;
}

4. Add Two Numbers Using Pointers Top ↑

#include <stdio.h>

int main(void) {
    int first = 100;
    int second = 200;
    int *p1 = &first;
    int *p2 = &second;

    printf("sum = %d\n", *p1 + *p2);
    return 0;
}

5. Swap Two User-Entered Numbers Top ↑

#include <stdio.h>

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main(void) {
    int first;
    int second;

    printf("Enter two integers: ");
    if (scanf("%d %d", &first, &second) != 2) {
        printf("Invalid input.\n");
        return 1;
    }

    swap(&first, &second);

    printf("first = %d, second = %d\n", first, second);
    return 0;
}

6. Return the Higher Value Through Pointer Parameters Top ↑

#include <stdio.h>

int highest(const int *a, const int *b) {
    return (*a > *b) ? *a : *b;
}

int main(void) {
    int first = 10;
    int second = 15;

    printf("Highest = %d\n", highest(&first, &second));
    return 0;
}

7. Display an Array Through a Pointer Top ↑

#include <stdio.h>

void display_array(const int *values, size_t count) {
    for (size_t i = 0; i < count; i++) {
        printf("Element: %d\n", values[i]);
    }
}

int main(void) {
    int values[] = {1, 2, 3, 4, 5};
    size_t count = sizeof values / sizeof values[0];

    display_array(values, count);
    return 0;
}

8. Increase Every Array Element by 2 Top ↑

#include <stdio.h>

void add_two(int *values, size_t count) {
    for (size_t i = 0; i < count; i++) {
        values[i] += 2;
    }
}

int main(void) {
    int values[] = {1, 2, 3, 4, 5};
    size_t count = sizeof values / sizeof values[0];

    add_two(values, count);

    for (size_t i = 0; i < count; i++) {
        printf("%d ", values[i]);
    }
    putchar('\n');

    return 0;
}

9. Highest and Lowest Values in an Array Top ↑

#include <stdio.h>

void min_max(
    const int *values,
    size_t count,
    int *minimum,
    int *maximum
) {
    *minimum = values[0];
    *maximum = values[0];

    for (size_t i = 1; i < count; i++) {
        if (values[i] < *minimum) {
            *minimum = values[i];
        }

        if (values[i] > *maximum) {
            *maximum = values[i];
        }
    }
}

int main(void) {
    int values[] = {-8, 12, 40, 3, 17};
    size_t count = sizeof values / sizeof values[0];
    int minimum;
    int maximum;

    min_max(values, count, &minimum, &maximum);

    printf("Highest = %d, Lowest = %d\n", maximum, minimum);
    return 0;
}

10. Sort an Array Using Pointers Top ↑

#include <stdio.h>

void sort_ascending(int *values, size_t count) {
    for (size_t i = 0; i < count; i++) {
        for (size_t j = i + 1; j < count; j++) {
            int *left = values + i;
            int *right = values + j;

            if (*left > *right) {
                int temp = *left;
                *left = *right;
                *right = temp;
            }
        }
    }
}

int main(void) {
    int values[] = {-34, 14, -11, 15, 0, -20};
    size_t count = sizeof values / sizeof values[0];

    sort_ascending(values, count);

    for (size_t i = 0; i < count; i++) {
        printf("%d\n", values[i]);
    }

    return 0;
}

Output

-34
-20
-11
0
14
15

11. Reverse a String Using a Pointer Top ↑

#include <stdio.h>
#include <string.h>

int main(void) {
    const char text[] = "welcome to plus2net";
    const char *ptr = text;
    size_t length = strlen(text);

    for (size_t i = length; i > 0; i--) {
        putchar(*(ptr + i - 1));
    }

    putchar('\n');
    return 0;
}

12. Print the Middle Character Top ↑

#include <stdio.h>
#include <string.h>

int main(void) {
    const char text[] = "plus2net";
    const char *ptr = text;
    size_t length = strlen(text);

    if (length == 0) {
        printf("String is empty.\n");
        return 0;
    }

    printf("Middle character: %c\n", *(ptr + length / 2));
    return 0;
}

For an even-length string, integer division selects the character immediately to the right of the halfway boundary, matching the original exercise requirement.

13. Search an Array Through a Pointer Top ↑

#include <stdio.h>

int contains(
    const int *values,
    size_t count,
    int target
) {
    for (size_t i = 0; i < count; i++) {
        if (values[i] == target) {
            return 1;
        }
    }

    return 0;
}

int main(void) {
    int values[] = {5, 12, 18, 21, 30};
    size_t count = sizeof values / sizeof values[0];

    printf("%s\n",
           contains(values, count, 18)
               ? "Value found"
               : "Value not found");

    return 0;
}




plus2net.com






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer