Pointer Arithmetic and Arrays in C Practice Questions with Solutions

Introduction

Pointer arithmetic is an important C concept that connects pointers and arrays. Since an array stores elements in consecutive memory locations, pointers can be used to move from one element to another. In this chapter, you will practice pointer increment, decrement, addition, subtraction, array traversal, finding elements, reversing an array, and comparing array elements using pointers. These examples are designed to make pointer arithmetic easy to understand before moving to advanced pointer concepts. Pointer Arithmetic and Arrays in C practice questions with solutions to help you understand the concepts.

Q1. Print Array Elements Using a Pointer

Problem Statement

Write a C program to print all elements of an integer array using a pointer.

C Program

#include <stdio.h>

int main()
{
    int numbers[] = {10, 20, 30, 40, 50};
    int *ptr = numbers;

    int i;

    for (i = 0; i < 5; i++)
    {
        printf("%d ", *(ptr + i));
    }

    return 0;
}

Sample Output

10 20 30 40 50

Explanation

The array is:

int numbers[] = {10, 20, 30, 40, 50};

We create a pointer:

int *ptr = numbers;

Here, ptr points to the first element of the array.

This:

*(ptr + i)

means:

  1. Move the pointer by i elements.
  2. Access the value at that location.

So:

*(ptr + 0) → 10
*(ptr + 1) → 20
*(ptr + 2) → 30
*(ptr + 3) → 40
*(ptr + 4) → 50

Concepts Covered

  • Arrays
  • Pointers
  • Pointer arithmetic
  • Array traversal
  • Dereferencing

Q2. Access Array Elements Using Pointer Increment

Problem Statement

Write a C program to print array elements by incrementing a pointer.

C Program

#include <stdio.h>

int main()
{
    int numbers[] = {5, 10, 15, 20, 25};
    int *ptr = numbers;
    int i;

    for (i = 0; i < 5; i++)
    {
        printf("%d ", *ptr);
        ptr++;
    }

    return 0;
}

Sample Output

5 10 15 20 25

Explanation

Initially:

ptr → 5

After:

ptr++;

the pointer moves to the next integer:

ptr → 10

Then:

ptr → 15
ptr → 20
ptr → 25

The important point is that ptr++ does not simply add one byte.

Because ptr is an int *, it moves by the size of an int.

Conceptually:

ptr
 ↓
[5] [10] [15] [20] [25]

Concepts Covered

  • Pointer increment
  • Array traversal
  • ptr++
  • Dereferencing

Q3. Print an Array in Reverse Using Pointer Decrement

Problem Statement

Write a C program to print an array in reverse order using pointer decrement.

C Program

#include <stdio.h>

int main()
{
    int numbers[] = {10, 20, 30, 40, 50};

    int *ptr = &numbers[4];

    while (ptr >= numbers)
    {
        printf("%d ", *ptr);
        ptr--;
    }

    return 0;
}

Sample Output

50 40 30 20 10

Explanation

The last element is:

numbers[4]

So:

int *ptr = &numbers[4];

makes ptr point to 50.

Then:

ptr--;

moves the pointer to the previous array element.

The sequence becomes:

50 → 40 → 30 → 20 → 10

This is useful for understanding how pointers can move backward through an array.

Concepts Covered

  • Pointer decrement
  • Reverse array traversal
  • ptr--
  • Array addresses

Q4. Find the Sum of Array Elements Using a Pointer

Problem Statement

Write a C program to calculate the sum of all elements of an array using a pointer.

C Program

#include <stdio.h>

int main()
{
    int numbers[] = {10, 20, 30, 40, 50};
    int *ptr = numbers;

    int sum = 0;
    int i;

    for (i = 0; i < 5; i++)
    {
        sum = sum + *ptr;
        ptr++;
    }

    printf("Sum = %d", sum);

    return 0;
}

Sample Output

Sum = 150

Explanation

The pointer starts at the first element:

10

Then each value is added:

sum = 0 + 10
sum = 10 + 20
sum = 30 + 30
sum = 60 + 40
sum = 100 + 50

Final result:

150

The pointer moves to the next element after every iteration.

Concepts Covered

  • Pointer increment
  • Array traversal
  • Sum of array
  • Dereferencing

Q5. Find the Largest Element Using a Pointer

Problem Statement

Write a C program to find the largest element in an array using pointer arithmetic.

C Program

#include <stdio.h>

int main()
{
    int numbers[] = {25, 80, 45, 90, 30};

    int *ptr = numbers;
    int largest = *ptr;

    int i;

    for (i = 1; i < 5; i++)
    {
        ptr++;

        if (*ptr > largest)
        {
            largest = *ptr;
        }
    }

    printf("Largest = %d", largest);

    return 0;
}

Sample Output

Largest = 90

Explanation

Initially:

int largest = *ptr;

So:

largest = 25

The pointer then moves through the remaining elements.

The program compares each value with largest.

When it finds:

90 > 80

it updates:

largest = 90

At the end, 90 is the largest element.

Concepts Covered

  • Pointer arithmetic
  • Array traversal
  • Comparison
  • Finding maximum

Q6. Find an Element in an Array Using a Pointer

Problem Statement

Write a C program to search for a number in an array using a pointer.

C Program

#include <stdio.h>

int main()
{
    int numbers[] = {10, 25, 40, 55, 70};
    int search = 40;

    int *ptr = numbers;
    int found = 0;
    int i;

    for (i = 0; i < 5; i++)
    {
        if (*ptr == search)
        {
            found = 1;
            break;
        }

        ptr++;
    }

    if (found == 1)
    {
        printf("Element found.");
    }
    else
    {
        printf("Element not found.");
    }

    return 0;
}

Sample Output

Element found.

Explanation

The pointer starts at:

10

Then the program checks:

10 == 40 → No
25 == 40 → No
40 == 40 → Yes

When the element is found:

break;

stops the loop.

Concepts Covered

  • Pointer traversal
  • Searching
  • break
  • Array elements

Q7. Calculate the Difference Between Two Array Elements Using Pointers

Problem Statement

Write a C program to calculate the difference between two selected array elements using pointers.

C Program

#include <stdio.h>

int main()
{
    int numbers[] = {10, 20, 30, 40, 50};

    int *ptr1 = &numbers[1];
    int *ptr2 = &numbers[4];

    int difference = *ptr2 - *ptr1;

    printf("Difference = %d", difference);

    return 0;
}

Sample Output

Difference = 30

Explanation

Here:

int *ptr1 = &numbers[1];

points to:

20

And:

int *ptr2 = &numbers[4];

points to:

50

Therefore:

*ptr2 - *ptr1

becomes:

50 - 20

which gives:

30

Concepts Covered

  • Pointer to array element
  • Dereferencing
  • Arithmetic using pointer values

Q8. Calculate the Number of Elements Between Two Pointers

Problem Statement

Write a C program to find the number of array positions between two pointers.

C Program

#include <stdio.h>
#include <stddef.h>

int main()
{
    int numbers[] = {10, 20, 30, 40, 50};

    int *start = &numbers[1];
    int *end = &numbers[4];

    ptrdiff_t difference = end - start;

    printf("Number of positions = %td", difference);

    return 0;
}

Sample Output

Number of positions = 3

Explanation

The pointers point to:

start → numbers[1] → 20
end   → numbers[4] → 50

The expression:

end - start

calculates the number of array elements between the two positions.

Here:

4 - 1 = 3

So the result is:

3

For pointer subtraction, the pointers must point into the same array (or one position past its end).

ptrdiff_t from <stddef.h> is the appropriate type for the result of subtracting two pointers.

Concepts Covered

  • Pointer subtraction
  • Pointer arithmetic
  • ptrdiff_t
  • Array positions

Q9. Modify All Array Elements Using a Pointer

Problem Statement

Write a C program to multiply every element of an array by 2 using a pointer.

C Program

#include <stdio.h>

int main()
{
    int numbers[] = {5, 10, 15, 20, 25};

    int *ptr = numbers;
    int i;

    for (i = 0; i < 5; i++)
    {
        *ptr = *ptr * 2;
        ptr++;
    }

    printf("Updated array:\n");

    for (i = 0; i < 5; i++)
    {
        printf("%d ", numbers[i]);
    }

    return 0;
}

Sample Output

Updated array:
10 20 30 40 50

Explanation

Initially:

5 10 15 20 25

The statement:

*ptr = *ptr * 2;

changes the current array element.

For example, when ptr points to 5:

*ptr = 5 * 2

so the first element becomes:

10

The pointer then moves to the next element.

This demonstrates that pointers can be used not only to read array elements but also to modify them.

Concepts Covered

  • Pointer modification
  • Array traversal
  • Dereferencing
  • Updating array elements

Q10. Reverse an Array Using Two Pointers

Problem Statement

Write a C program to reverse an array using two pointers.

C Program

#include <stdio.h>

int main()
{
    int numbers[] = {10, 20, 30, 40, 50};

    int *left = &numbers[0];
    int *right = &numbers[4];

    int temp;

    while (left < right)
    {
        temp = *left;
        *left = *right;
        *right = temp;

        left++;
        right--;
    }

    printf("Reversed array:\n");

    for (int i = 0; i < 5; i++)
    {
        printf("%d ", numbers[i]);
    }

    return 0;
}

Sample Output

Reversed array:
50 40 30 20 10

Explanation

We use two pointers:

left  → first element
right → last element

Initially:

10 20 30 40 50
↑           ↑
left       right

We swap the values:

50 20 30 40 10

Then:

left++;
right--;

Now:

50 20 30 40 10
   ↑       ↑
  left   right

They continue moving toward the center.

The final result is:

50 40 30 20 10

This is an important practical example of pointer arithmetic.

Concepts Covered

  • Two pointers
  • Pointer increment
  • Pointer decrement
  • Swapping
  • Array reversal

Key Takeaways

  • Pointer arithmetic allows pointers to move through arrays.
  • ptr++ moves a pointer to the next element.
  • ptr-- moves a pointer to the previous element.
  • ptr + n moves forward by n elements.
  • ptr - n moves backward by n elements.
  • Pointer movement depends on the pointer’s data type.
  • *(ptr + i) can be used to access array elements.
  • Array indexing and pointer arithmetic are closely related.
  • Two pointers can be used to reverse an array efficiently.
  • Pointer subtraction can determine the distance between positions in the same array.
  • ptr++ and (*ptr)++ have completely different meanings.
  • Pointers should always be valid before they are dereferenced.
  • Pointer arithmetic is an important foundation for advanced C programming.

FAQs

1. What is pointer arithmetic in C?

Pointer arithmetic means performing operations such as increment, decrement, addition, or subtraction on pointers to move between elements of an array or another suitable object.

2. What does ptr++ do in C?

ptr++ moves the pointer to the next element of the type it points to.

For an int *, it moves to the next int.

3. How can I access an array element using a pointer?

You can use:

*(ptr + index)

For example:

int numbers[] = {10, 20, 30};
int *ptr = numbers;

printf("%d", *(ptr + 1));

Output:

20

4. What is the difference between ptr++ and (*ptr)++?

ptr++ moves the pointer to the next element.

(*ptr)++ increases the value of the element currently pointed to by the pointer.

5. Can two pointers point to the same array?

Yes. Multiple pointers can point to elements of the same array.

int *p1 = &numbers[0];
int *p2 = &numbers[2];

Each pointer can point to a different element of the same array.

6. Can pointers be subtracted in C?

Yes, pointers to elements of the same array can be subtracted.

ptrdiff_t difference = end - start;

The result represents the number of array elements between the two positions.

7. Why are pointers useful with arrays?

Pointers provide another way to traverse and modify array elements and form an important foundation for functions, strings, dynamic memory, and advanced data structures.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top