Pointers in C Practice Questions with Solutions

Introduction

Pointers are one of the most important concepts in C because they allow a program to work directly with memory addresses. A pointer stores the address of another variable instead of storing the variable’s value itself. In this chapter, you will learn pointers from the beginning through simple practice programs covering addresses, dereferencing, changing values through pointers, multiple pointers, pointer data types, and practical examples. These examples build the foundation for arrays, functions, dynamic memory, and structures. Pointers in C practice questions with solutions to help you understand the concepts.

Q1. Store the Address of a Variable in a Pointer

Problem Statement

Write a C program to create an integer variable and store its address in a pointer.

C Program

#include <stdio.h>

int main()
{
    int number = 10;
    int *ptr;

    ptr = &number;

    printf("Value of number = %d\n", number);
    printf("Address of number = %p\n", (void *)&number);
    printf("Address stored in ptr = %p", (void *)ptr);

    return 0;
}

Sample Output

The exact memory address can be different each time:

Value of number = 10
Address of number = 0x7ff...
Address stored in ptr = 0x7ff...

Explanation

This creates an integer variable:

int number = 10;

Then we create an integer pointer:

int *ptr;

The * tells C that ptr is a pointer to an integer.

This statement stores the address of number:

ptr = &number;

The & operator means address of.

So:

&amp;number

means:

Give me the memory address of number.

The pointer now stores that address.

Concepts Covered

  • Pointer declaration
  • Address operator &
  • Memory address
  • %p
  • Integer pointer

Q2. Access a Variable’s Value Using a Pointer

Problem Statement

Write a C program to access the value of a variable using a pointer.

C Program

#include <stdio.h>

int main()
{
    int number = 25;
    int *ptr = &number;

    printf("Value of number = %d\n", number);
    printf("Value using pointer = %d", *ptr);

    return 0;
}

Sample Output

Value of number = 25
Value using pointer = 25

Explanation

Here:

int *ptr = &number;

means ptr stores the address of number.

To access the value stored at that address, we use:

*ptr

This is called dereferencing.

So:

number → 25
ptr    → address of number
*ptr   → 25

The * has two important uses:

int *ptr;

Here it declares a pointer.

*ptr

Here it accesses the value stored at the pointer’s address.

Concepts Covered

  • Pointer
  • Dereferencing
  • & operator
  • * operator

Q3. Change a Variable’s Value Using a Pointer

Problem Statement

Write a C program to change the value of a variable using a pointer.

C Program

#include <stdio.h>

int main()
{
    int number = 10;
    int *ptr = &number;

    printf("Before = %d\n", number);

    *ptr = 50;

    printf("After = %d", number);

    return 0;
}

Sample Output

Before = 10
After = 50

Explanation

Initially:

number = 10

The pointer stores the address of number:

int *ptr = &number;

Then:

*ptr = 50;

means:

Go to the address stored in ptr and change the value there to 50.

Since that address belongs to number, the value of number changes.

Before:
number = 10

After:
number = 50

This is one of the most important ideas behind pointers.

Concepts Covered

  • Dereferencing
  • Modifying values through pointers
  • Memory addresses

Q4. Use Pointers with Different Data Types

Problem Statement

Write a C program that creates pointers for int, float, and char variables and prints their values.

C Program

#include <stdio.h>

int main()
{
    int age = 20;
    float price = 99.50f;
    char grade = 'A';

    int *pAge = &age;
    float *pPrice = &price;
    char *pGrade = &grade;

    printf("Age = %d\n", *pAge);
    printf("Price = %.2f\n", *pPrice);
    printf("Grade = %c", *pGrade);

    return 0;
}

Sample Output

Age = 20
Price = 99.50
Grade = A

Explanation

A pointer should normally have a type compatible with the object it points to.

For the integer:

int *pAge = &age;

For the float:

float *pPrice = &price;

For the character:

char *pGrade = &grade;

The pointer type tells the compiler how to interpret the object at that address.

Concepts Covered

  • int pointer
  • float pointer
  • char pointer
  • Dereferencing

Q5. Print Value, Address and Pointer Value

Problem Statement

Write a C program that prints the value of a variable, its address, and the value stored in its pointer.

C Program

#include <stdio.h>

int main()
{
    int number = 100;
    int *ptr = &number;

    printf("Value of number = %d\n", number);
    printf("Address of number = %p\n", (void *)&number);
    printf("Value stored in ptr = %p\n", (void *)ptr);
    printf("Value using *ptr = %d", *ptr);

    return 0;
}

Sample Output

The address will vary:

Value of number = 100
Address of number = 0x7ff...
Value stored in ptr = 0x7ff...
Value using *ptr = 100

Explanation

There are three different things to understand:

number

gives the value:

100
&amp;number

gives the address of number.

ptr

contains that same address.

*ptr

gets the value stored at that address.

So remember:

number  → value
&amp;number → address
ptr     → address stored in pointer
*ptr    → value at that address

Concepts Covered

  • Value
  • Address
  • Pointer
  • Dereferencing
  • %p

Q6. Use Two Pointers with the Same Variable

Problem Statement

Write a C program where two pointers point to the same integer variable.

C Program

#include <stdio.h>

int main()
{
    int number = 75;

    int *ptr1 = &number;
    int *ptr2 = &number;

    printf("Using ptr1 = %d\n", *ptr1);
    printf("Using ptr2 = %d", *ptr2);

    return 0;
}

Sample Output

Using ptr1 = 75
Using ptr2 = 75

Explanation

Both pointers store the address of the same variable:

int *ptr1 = &number;
int *ptr2 = &number;

Therefore:

*ptr1

and:

*ptr2

both access the same number.

If we change the value using either pointer:

*ptr1 = 200;

then:

*ptr2

will also produce:

200

because both pointers refer to the same object.

Concepts Covered

  • Multiple pointers
  • Same memory address
  • Dereferencing

Q7. Swap Two Numbers Using Pointers

Problem Statement

Write a C program to swap two numbers using pointers.

C Program

#include <stdio.h>

int main()
{
    int first = 10;
    int second = 20;
    int temp;

    int *pFirst = &first;
    int *pSecond = &second;

    printf("Before swapping:\n");
    printf("First = %d\n", first);
    printf("Second = %d\n", second);

    temp = *pFirst;
    *pFirst = *pSecond;
    *pSecond = temp;

    printf("\nAfter swapping:\n");
    printf("First = %d\n", first);
    printf("Second = %d", second);

    return 0;
}

Sample Output

Before swapping:
First = 10
Second = 20

After swapping:
First = 20
Second = 10

Explanation

Initially:

first  = 10
second = 20

The pointers point to these variables.

First:

temp = *pFirst;

stores 10 in temp.

Then:

*pFirst = *pSecond;

changes first to 20.

Finally:

*pSecond = temp;

changes second to 10.

The result is:

first  = 20
second = 10

This technique becomes especially important when pointers are passed to functions.

Concepts Covered

  • Pointers
  • Dereferencing
  • Swapping
  • Temporary variable

Q8. Find the Larger Number Using Pointers

Problem Statement

Write a C program to find the larger of two numbers using pointers.

C Program

#include <stdio.h>

int main()
{
    int first = 45;
    int second = 72;

    int *pFirst = &first;
    int *pSecond = &second;

    if (*pFirst > *pSecond)
    {
        printf("Larger number = %d", *pFirst);
    }
    else
    {
        printf("Larger number = %d", *pSecond);
    }

    return 0;
}

Sample Output

Larger number = 72

Explanation

Instead of directly comparing:

first > second

we compare the values through pointers:

*pFirst > *pSecond

The * retrieves the value stored at the address.

Here:

*pFirst  = 45
*pSecond = 72

Therefore, 72 is larger.

Concepts Covered

  • Pointer dereferencing
  • Comparison
  • if-else

Q9. Use a Pointer to Traverse an Array

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 name:

numbers

represents the address of its first element in this context.

So:

int *ptr = numbers;

makes ptr point to the first element.

The expression:

*(ptr + i)

moves the pointer to the appropriate array element and then dereferences it.

For example:

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

Pointer arithmetic automatically takes the size of the pointed-to type into account.

Concepts Covered

  • Pointers and arrays
  • Pointer arithmetic
  • Dereferencing
  • Array traversal

Q10. Pointer to Pointer

Problem Statement

Write a C program to demonstrate a pointer that stores the address of another pointer.

C Program

#include <stdio.h>

int main()
{
    int number = 100;

    int *ptr = &number;
    int **ptr2 = &ptr;

    printf("Number = %d\n", number);
    printf("Using ptr = %d\n", *ptr);
    printf("Using ptr2 = %d", **ptr2);

    return 0;
}

Sample Output

Number = 100
Using ptr = 100
Using ptr2 = 100

Explanation

We have three levels:

number
   ↓
 ptr
   ↓
 ptr2

First:

int *ptr = &number;

ptr stores the address of number.

Then:

int **ptr2 = &ptr;

ptr2 stores the address of ptr.

Therefore:

*ptr

gives the value of number.

And:

**ptr2

goes through two pointer levels and finally gives the value of number.

Think of it as:

ptr2  → ptr → number
        ↓     ↓
      address 100

Concepts Covered

  • Pointer to pointer
  • **
  • Multiple levels of dereferencing
  • Memory addresses

Key Takeaways

  • A pointer stores the address of another object.
  • & is used to obtain an object’s address.
  • * is used to dereference a pointer.
  • ptr and *ptr have different meanings.
  • %p is used to print pointer addresses.
  • A pointer should be initialized to a valid address before it is dereferenced.
  • Pointer types normally match the type of the object they point to.
  • Multiple pointers can point to the same variable.
  • Pointers can be used to modify variables indirectly.
  • Pointers and arrays are closely related in C.
  • Pointer arithmetic is based on the size of the pointed-to type.
  • A pointer can also point to another pointer.
  • Pointers are fundamental to functions, arrays, strings, structures, and dynamic memory allocation.

FAQs

1. What is a pointer in C?

A pointer is a variable that stores the memory address of another object.

Example:

int number = 10;
int *ptr = &number;

Here, ptr stores the address of number.

2. What is the difference between & and * in C?

& obtains an address:

&number

* dereferences a pointer and accesses the value at its stored address:

*ptr

3. How do I print a pointer address in C?

Use %p and cast the pointer to void *:

printf("%p", (void *)ptr);

The exact address can change between program executions.

4. What does dereferencing a pointer mean?

Dereferencing means accessing the object stored at the address contained in the pointer.

Example:

int number = 25;
int *ptr = &number;

printf("%d", *ptr);

The output is:

25

5. Can a pointer change the value of a variable?

Yes. If the pointer points to a valid variable, you can modify that variable through the pointer.

int number = 10;
int *ptr = &number;

*ptr = 50;

Now number is 50.

6. What is a pointer to a pointer?

A pointer to a pointer stores the address of another pointer.

Example:

int number = 10;
int *ptr = &number;
int **ptr2 = &ptr;

Here, ptr2 is a pointer to ptr.

7. Why are pointers important in C?

Pointers provide direct access to memory addresses and are essential for arrays, functions, strings, structures, dynamic memory allocation, and many data structures.

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

Scroll to Top