Question
I am just starting to learn pointers in C, and I am confused about when to use & and *.
I understand that & gives the address of a variable, and * can be used with a pointer to access the value stored at the address it points to. However, things seem different when working with arrays, strings, or when passing pointers to functions. Because of that, I am having trouble seeing a consistent pattern.
When should I use & and * in C?
Short Answer
By the end of this page, you will understand the core roles of & and * in C, how they behave with normal variables, pointers, arrays, strings, and functions, and how to recognize the pattern behind pointer syntax instead of memorizing special cases.
Concept
In C, & and * are closely related operators used to work with memory addresses.
&means "give me the address of this object".*means "work with the object stored at this address".
The confusion usually comes from the fact that * has two different jobs depending on where it appears:
- In a declaration, it means "this variable is a pointer".
- In an expression, it means "dereference this pointer" and access the value being pointed to.
The core idea
A normal variable stores a value:
int x = 10;
A pointer stores the address of another object:
int *p = &x;
Here is what happens:
xis anint&xis the address ofxpis a pointer to
Mental Model
Think of a variable as a house, and a pointer as a note containing the house address.
- The variable
xis the actual house. &xis the street address of that house.- A pointer like
pis a piece of paper storing that address. *pmeans "go to the address written on the paper and enter the house".
Example with the analogy
int x = 42;
int *p = &x;
This means:
xis a house containing42&xis the address of that housepstores that address*plets you read or change what is inside the house
*p = 100;
Now the house x contains 100.
Why arrays seem special
An array is like a row of houses standing next to each other.
Syntax and Examples
Basic syntax
1. Declaring a pointer
int *p;
This means p is a pointer to int.
2. Storing an address in a pointer
int x = 10;
int *p = &x;
&xgets the address ofxpstores that address
3. Reading the value through a pointer
printf("%d\n", *p);
This prints 10.
4. Writing through a pointer
*p = 25;
printf("%d\n", x);
This changes x to 25.
Step by Step Execution
Consider this example:
#include <stdio.h>
void double_value(int *p) {
*p = *p * 2;
}
int main(void) {
int x = 6;
int *ptr = &x;
double_value(ptr);
printf("x = %d\n", x);
return 0;
}
Step-by-step trace
1. int x = 6;
A normal integer variable x is created and stores 6.
2. int *ptr = &x;
&xgets the address ofxptrstores that addressptrnow points tox
3.
Real World Use Cases
Modifying values inside functions
Pointers let a function update caller-owned data.
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
Used for:
- swapping numbers
- returning multiple results
- updating counters or flags
Working with arrays efficiently
Arrays are often passed to functions as pointers to the first element.
int sum(int *arr, int size) {
int total = 0;
for (int i = 0; i < size; i++) {
total += arr[i];
}
return total;
}
Used in:
- statistics
- parsing input
- image and signal processing
Handling strings
C strings are arrays of characters, so pointers are everywhere.
void print_first_char(char *s) {
(s != ) {
(, *s);
}
}
Real Codebase Usage
In real C projects, developers do not use pointers only in toy examples. They use them as part of common patterns.
Output parameters
A function may return success or failure and write the actual result through a pointer.
int divide(int a, int b, int *result) {
if (b == 0) {
return 0;
}
*result = a / b;
return 1;
}
This pattern is common when a function needs to return:
- a status code
- computed output
- multiple values
Guard clauses for pointer safety
Real code often checks pointers before dereferencing them.
void print_number(const int *p) {
if (p == NULL) {
return;
}
printf("%d\n", *p);
}
Array processing APIs
Many functions accept a pointer plus a size.
int {
(arr == || size <= ) {
;
}
max = arr[];
( i = ; i < size; i++) {
(arr[i] > max) {
max = arr[i];
}
}
max;
}
Common Mistakes
1. Dereferencing an uninitialized pointer
Broken code:
int *p;
*p = 10;
Why it is wrong:
pdoes not point to valid memory- dereferencing it causes undefined behavior
Fix:
int x = 10;
int *p = &x;
2. Forgetting & when a function expects a pointer
Broken code:
void set_zero(int *p) {
*p = 0;
}
int x = 5;
set_zero(x);
Why it is wrong:
set_zeroexpects an addressxis an integer value, not an address
Fix:
set_zero(&x);
3. Forgetting * when accessing the pointed value
Comparisons
| Concept | Meaning | Example | Notes |
|---|---|---|---|
x | the value/object itself | int x = 10; | normal variable |
&x | address of x | int *p = &x; | use when you need a pointer to x |
p | pointer value (an address) | int *p = &x; | stores where something lives |
*p | value at the address in p |
Cheat Sheet
Quick rules
&var-> address ofvar*ptr-> value stored at the address inptrtype *ptr;-> declares a pointer- pass
&xto a function when it needs to modifyx - use
*pinside the function to read or write the original value
Common patterns
int x = 5;
int *p = &x;
printf("%d\n", *p); // value of x
printf("%p\n", (void*)p); // address of x
*p = 10; // changes x
Arrays
int arr[3] = {, , };
arr
*arr
FAQ
When do I use & in C?
Use & when you need the address of an existing variable, usually to store it in a pointer or pass it to a function that expects a pointer.
When do I use * in C?
Use * in a declaration to declare a pointer, and use * in an expression to access the value stored at the address held by that pointer.
Why do arrays often work without &?
Because in many expressions, an array name automatically behaves like a pointer to its first element.
Is an array the same as a pointer in C?
No. They are related, and arrays often decay to pointers in expressions, but they are different types.
Why do functions use pointers to change variables?
C passes arguments by value. Passing a pointer gives the function the address of the original variable, so it can modify the original data through dereferencing.
What does *p = 5 mean?
It means "store 5 in the object pointed to by p". It changes the value at that memory location.
What is the difference between p and *p?
Mini Project
Description
Build a small C program that updates variables through pointers and processes an array through a function. This project helps you practice when to pass a normal value, when to pass an address with &, and when to use * inside a function to modify the original data.
Goal
Create a program that increments a number, swaps two values, and prints the first element of an array using pointer-based functions.
Requirements
- Create a function that increments an integer using a pointer parameter.
- Create a function that swaps two integers using pointer parameters.
- Create a function that prints the first element of an integer array.
- In
main, declare variables and call the functions correctly using&where needed. - Print results before and after the changes to verify that the original variables were modified.
Keep learning
Related questions
Array-to-Pointer Conversion in C and C++ Explained
Learn what array-to-pointer conversion means in C and C++, how array decay works, and how it differs from a pointer to an array.
Building More Fault-Tolerant Embedded C++ Applications for Radiation-Prone ARM Systems
Learn practical C++ and compile-time techniques to reduce soft-error damage in embedded ARM systems exposed to radiation.
C Pointer to Array vs Array of Pointers: How to Read Complex Declarations
Learn the difference between pointer-to-array and array-of-pointers in C, plus a simple rule for reading complex declarations correctly.