Skip to content
StudyFame

Pointers in C

Every variable lives somewhere in memory, like every house has an address. A pointer is a variable that stores such an address. With it, you can reach and change the original variable from anywhere.

Pointers are the most powerful feature of C, and the one students fear most. This lesson keeps it simple: two operators, & and *, one memory picture, and clear examples. Everything else in this module builds on these.

What You Will Learn

  • What memory addresses are.
  • What a pointer is.
  • The address-of operator &.
  • The value-at (dereference) operator *.
  • How to change a variable through a pointer.
  • Why pointers are used in C.

What is a Pointer in C?

Exam definition: A pointer is a variable that stores the memory address of another variable. The & (address-of) operator gives a variable's address, and the * (indirection or dereference) operator gives the value stored at an address.

Memory trick: A variable is a house, its value is the person living inside, and a pointer is a slip of paper with the house address on it. & = "write down the address of this house". * = "go to that address and see who is inside".

Memory picture

int age = 21;
int *p = &age;

   age (an int)                p (a pointer to int)
  +----------+                +----------+
  |    21    |  <-----------  |   1000   |
  +----------+                +----------+
  address 1000                address 2000

age  = 21        &age = 1000
p    = 1000      *p   = 21      &p = 2000

Real addresses are large numbers like 0x7ffd5c2a and change every time you run the program. We use 1000 and 2000 to keep the picture simple.

Syntax of Pointer Operators

int x = 10;
int *p;        /* p is a pointer to int (the * here means "pointer") */
p = &x;        /* p now holds the address of x */
*p = 20;       /* go to that address, store 20: x becomes 20 */

printf("%p", (void *) p);   /* print an address with %p */
ExpressionRead it asValue in the picture
agevalue of age21
&ageaddress of age1000
pvalue of p (an address)1000
*pvalue at the address in p21
&paddress of p itself2000

Example 1: Your First Pointer Program

#include <stdio.h>

int main(void)
{
    int age = 21;
    int *p = &age;             /* p points to age */

    printf("age            = %d\n", age);
    printf("*p             = %d\n", *p);
    printf("p == &age ?    = %s\n", p == &age ? "yes" : "no");

    *p = 22;                   /* change age through p */
    printf("after *p = 22, age = %d\n", age);

    age = 30;                  /* change age directly */
    printf("after age = 30, *p = %d\n", *p);

    return 0;
}

Output

age            = 21
*p             = 21
p == &age ?    = yes
after *p = 22, age = 22
after age = 30, *p = 30

Code Explanation

  1. int *p = &age; creates a pointer p and stores the address of age in it. Now "p points to age".
  2. *p follows the address and reads the value there: 21, the same as age.
  3. p == &age is true: p holds exactly the address of age.
  4. *p = 22; writes 22 at that address, so age itself changes.
  5. Changing age directly is also seen through *p: both name the same box in memory.

How the Two Meanings of * Work

The star has two jobs, depending on where it appears:

WhereExampleMeaning
In a declarationint *p;"p is a pointer to int"
In an expression*p = 5; or x = *p;"the value at the address in p"

& and * are opposites: *&age is just age. Taking the address, then going to it, brings you back.

Example 2: One Function Changes Two Variables

A normal function returns only one value. With pointers, it can update many variables in the caller. Here a shop bill is split into rupees and paise.

#include <stdio.h>

void split(int total_paise, int *rupees, int *paise)
{
    *rupees = total_paise / 100;    /* write into caller's variables */
    *paise = total_paise % 100;
}

int main(void)
{
    int r, ps;

    split(12575, &r, &ps);         /* send the addresses */
    printf("12575 paise = Rs %d and %d paise\n", r, ps);

    split(990, &r, &ps);
    printf("990 paise   = Rs %d and %d paise\n", r, ps);

    return 0;
}

Output

12575 paise = Rs 125 and 75 paise
990 paise   = Rs 9 and 90 paise

This is the same idea as call by reference and as scanf("%d", &x): you give the address so the function can fill the variable.

Why Pointers are Used in C

  • Change caller's variables from a function (swap, scanf, returning many results).
  • Pass large data fast: send one address instead of copying a big array or structure.
  • Arrays and strings: array names work like pointers; string functions use them.
  • Dynamic memory: malloc returns a pointer to memory taken at run time (Module 12).
  • Data structures: linked lists, trees and graphs are built by linking nodes with pointers (Module 16).
  • Hardware access: embedded and system programs reach exact memory locations.

Important Points to Remember

  • A pointer stores an address, not a normal value.
  • &x = address of x; *p = value at address p.
  • In a declaration, * means "is a pointer".
  • Changing *p changes the variable p points to.
  • Print addresses with %p (cast to void *).
  • Addresses change every run; never rely on their exact value.

Common Mistakes Beginners Make

MistakeWhat happensFix
int *p; *p = 5; (p not set)Writes to a random address: crashPoint it first: p = &x;
p = x; instead of p = &x;Stores a value as an address: warning, crashUse &x
Printing an address with %dWrong or cut-off numberUse %p
Confusing p and *pPrints an address instead of the value*p for the value
Thinking int *p stores an intWrong mental modelIt stores the address of an int

Pointers in C Practice Questions

  1. Define a pointer.
  2. What do the operators & and * do?
  3. If int x = 7; int *p = &x;, what are *p and *&x?
  4. After *p = *p + 3; (same p), what is x?
  5. Give three uses of pointers.
Show answers
  1. A variable that stores the memory address of another variable.
  2. & gives the address of a variable; * gives the value stored at an address.
  3. Both are 7.
  4. 10.
  5. Changing caller variables in functions, dynamic memory allocation, building linked lists (also: fast passing of arrays, strings).

Coding Exercise

Write a function void min_max(int a, int b, int c, int *min, int *max) that stores the smallest and largest of three numbers. Test with 14, 3, 27.

Show solution
#include <stdio.h>

void min_max(int a, int b, int c, int *min, int *max)
{
    *min = a;
    *max = a;
    if (b < *min) *min = b;
    if (c < *min) *min = c;
    if (b > *max) *max = b;
    if (c > *max) *max = c;
}

int main(void)
{
    int lo, hi;
    min_max(14, 3, 27, &lo, &hi);
    printf("Smallest = %d, Largest = %d\n", lo, hi);
    return 0;
}
Smallest = 3, Largest = 27

Compare with largest of three numbers using pointer.

Quick Quiz: Pointers in C (MCQ)

  1. A pointer stores:

    A) A value   B) An address   C) A data type   D) A function name only

  2. Which operator gives the address of a variable?

    A) *   B) &   C) ->   D) %

  3. int x = 5, *p = &x; *p = 9; Now x is:

    A) 5   B) 9   C) Address   D) Error

  4. Format specifier to print an address:

    A) %d   B) %a   C) %p   D) %x only

  5. *&x is equal to:

    A) &x   B) x   C) 0   D) Error

Show answers
  1. B: an address.
  2. B: &.
  3. B: 9.
  4. C: %p.
  5. B: x.

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

PointerVariable that stores an address
&xAddress of x
*pValue at address p (dereference)
int *p;p is a pointer to int
*&xSame as x
Print address%p

Frequently asked questions

What is a pointer in C?
A variable that holds the memory address of another variable.
What is dereferencing?
Using *p to reach the value stored at the address in p.
Why do we use pointers?
To change caller variables, pass big data fast, use dynamic memory and build linked data structures.

Remember

  • Pointer = slip with a house address.
  • & gets the address; * goes inside.

Next Topic

Next lesson: Declaring and Using Pointers. You will declare pointers of different types, initialize them safely and avoid the classic beginner traps.