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 */
| Expression | Read it as | Value in the picture |
|---|---|---|
age | value of age | 21 |
&age | address of age | 1000 |
p | value of p (an address) | 1000 |
*p | value at the address in p | 21 |
&p | address of p itself | 2000 |
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
int *p = &age;creates a pointerpand stores the address ofagein it. Now "p points to age".*pfollows the address and reads the value there: 21, the same asage.p == &ageis true: p holds exactly the address of age.*p = 22;writes 22 at that address, soageitself changes.- Changing
agedirectly 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:
| Where | Example | Meaning |
|---|---|---|
| In a declaration | int *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:
mallocreturns 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
| Mistake | What happens | Fix |
|---|---|---|
int *p; *p = 5; (p not set) | Writes to a random address: crash | Point it first: p = &x; |
p = x; instead of p = &x; | Stores a value as an address: warning, crash | Use &x |
Printing an address with %d | Wrong or cut-off number | Use %p |
Confusing p and *p | Prints an address instead of the value | *p for the value |
Thinking int *p stores an int | Wrong mental model | It stores the address of an int |
Pointers in C Practice Questions
- Define a pointer.
- What do the operators
&and*do? - If
int x = 7; int *p = &x;, what are*pand*&x? - After
*p = *p + 3;(same p), what is x? - Give three uses of pointers.
Show answers
- A variable that stores the memory address of another variable.
&gives the address of a variable;*gives the value stored at an address.- Both are 7.
- 10.
- 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)
-
A pointer stores:
A) A value B) An address C) A data type D) A function name only
-
Which operator gives the address of a variable?
A)
*B)&C)->D)% -
int x = 5, *p = &x; *p = 9;Now x is:A) 5 B) 9 C) Address D) Error
-
Format specifier to print an address:
A)
%dB)%aC)%pD)%xonly -
*&xis equal to:A)
&xB)xC) 0 D) Error
Show answers
- B: an address.
- B:
&. - B: 9.
- C:
%p. - B:
x.
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Pointer | Variable that stores an address |
|---|---|
| &x | Address of x |
| *p | Value at address p (dereference) |
| int *p; | p is a pointer to int |
| *&x | Same 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.