What is a Pointer?

A pointer is a special variable that stores the memory address of another variable instead of storing the actual value.

Definition:

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


Example

#include <stdio.h>

int main()
{
    int a = 10;      // Normal integer variable
    int *p;          // Pointer variable

    p = &a;          // Store the address of 'a' in pointer 'p'

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

    return 0;
}

Output (Example)

Value of a = 10
Address of a = 0x7ffeefbff5ac
Value stored in p = 0x7ffeefbff5ac
Value pointed by p = 10

Note: The memory address will be different every time you run the program.


Step-by-Step Explanation

Step 1: Declare a normal variable

int a = 10;
  • Variable name: a
  • Value: 10
  • It occupies a memory location (address).

Step 2: Declare a pointer

int *p;
  • p is a pointer variable.
  • * indicates that p is a pointer.
  • int means it can store the address of an integer variable.

Step 3: Store the address

p = &a;
  • &a means address of a.
  • This address is stored in pointer p.

Step 4: Access the value

*p

The * operator is called the dereference operator.

It means:

Go to the address stored in p and retrieve the value stored there.

If:

a = 10
Address of a = 1000
p = 1000

Then:

*p = 10

Memory Diagram

        Memory

Address      Value
----------------------
1000         10
             โ†‘
             |
             p

Or more clearly:

+-----------+        +-----------+
|     p     | -----> |     a     |
|  Address  |        | Value=10  |
+-----------+        +-----------+

Changing the Value Using a Pointer

#include <stdio.h>

int main()
{
    int a = 10;
    int *p = &a;

    *p = 50;

    printf("a = %d", a);

    return 0;
}

Output

a = 50

Explanation:

  • p stores the address of a.
  • *p = 50 changes the value stored at that address.
  • Therefore, the value of a becomes 50.

Important Operators

OperatorMeaningExample
&Address-of operator&a gives the address of a
*Dereference operator*p gives the value stored at the address

Quick Summary

ExpressionMeaning
aValue of the variable
&aAddress of the variable
pAddress stored in the pointer
*pValue stored at that address

Real-Life Analogy

Imagine a house:

  • House โ†’ Variable (a)
  • House Address โ†’ Memory address (&a)
  • Paper containing the house address โ†’ Pointer (p)
  • Going to the address and entering the house โ†’ Dereferencing (*p)

A pointer doesn’t store the house itself; it stores where the house is located.


Exam Definition

A pointer is a variable that stores the memory address of another variable. It allows indirect access to the value stored at that memory location.

Related Posts