Mastering C Pointers: A Practical, Expert‑Guided Tutorial
Mastering C Pointers
Explore the core concepts of pointers in C, from memory addresses to practical examples and common pitfalls. Written for developers who want to deepen their understanding with real‑world code.
Pointers are a cornerstone of efficient C programming. Before diving into pointers, let’s review how memory addresses work in C.
Memory Addresses in C
If you have a variable var, the expression &var yields the address where var is stored.
Address manipulation appears frequently, for instance in scanf():
scanf("%d", &var);
This stores the user’s input directly into the memory location of var. A quick demo:
#include <stdio.h>
int main() {
int var = 5;
printf("var: %d\n", var);
printf("address of var: %p", &var);
return 0;
}
Output
var: 5 address of var: 0x7fffc1a2b1c8
Note: The address will differ on each run and system.
What Is a Pointer?
A pointer is a variable that holds a memory address rather than a conventional value. By storing addresses, pointers allow indirect access to data, enabling dynamic memory management, efficient data structures, and low‑level system control.
Declaring Pointers
Typical declaration syntax:
int *p;
Variations are also common:
int *p1;
int *p2;
Note that spacing around the asterisk does not affect the declaration. However, be careful with comma‑separated declarations:
int *p1, p2; /* p1 is a pointer, p2 is an int */
Assigning Addresses to Pointers
Example:
int *pc, c;
c = 5;
pc = &c;
Here, c receives the value 5, and pc stores the address of c.
Dereferencing Pointers
To access the value stored at the address a pointer references, use the dereference operator *:
int *pc, c;
c = 5;
pc = &c;
printf("%d", *pc); /* Prints 5 */
Remember, pc is the pointer; *pc retrieves the value it points to. Attempting to assign an address to *pc (e.g., *pc = &c;) is invalid.
The * operator is known as the dereference operator.
Modifying Values Through Pointers
You can change the underlying data via its pointer:
int *pc, c;
c = 5;
pc = &c;
*pc = 1; /* c becomes 1 */
printf("%d", c); /* Prints 1 */
Multiple variables can share the same pointer after reassignment:
int *pc, c, d;
c = 5;
d = -15;
pc = &c; printf("%d", *pc); /* 5 */
pc = &d; printf("%d", *pc); /* -15 */
Practical Example
Run this full program to observe how pointer values remain constant while the data they reference can change:
#include <stdio.h>
int main() {
int *pc, c;
c = 22;
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c);
pc = &c;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc);
c = 11;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc);
*pc = 2;
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c);
return 0;
}
Output
Address of c: 0x7fffc1a2b1c8 Value of c: 22 Address of pointer pc: 0x7fffc1a2b1c8 Content of pointer pc: 22 Address of pointer pc: 0x7fffc1a2b1c8 Content of pointer pc: 11 Address of c: 0x7fffc1a2b1c8 Value of c: 2
Explanation
int *pc, c;– declares a pointer pc and an int c.c = 22;– stores 22 at c’s memory.pc = &c;– points pc to c.c = 11;– updates the value via direct access.*pc = 2;– changes the value through the pointer.
Common Mistakes with Pointers
Typical errors include mixing up pointers and values:
int c, *pc;
// Incorrect – assigns a value to a pointer
pc = c; // Error
// Incorrect – attempts to assign an address to the dereferenced pointer
*pc = &c; // Error
// Correct – assign address to pointer
pc = &c; // OK
// Correct – assign value to the location pointed to
*pc = c; // OK
For beginners, the syntax int *p = &c; can be confusing. It is equivalent to:
int *p;
p = &c;
Writing it as int* p = &c; reduces ambiguity.
Now that you understand pointers, you’ll be ready to explore their relationship with arrays in the next tutorial.
C Language
- Understanding C++ Pointers: A Practical Guide
- C++ Pointers and Arrays: Mastering the Relationship
- Understanding the Relationship Between Arrays and Pointers in C
- C Programming: Passing Addresses & Pointers to Functions – A Practical Guide
- Mastering C Structs and Pointers: A Practical Guide
- Mastering C++ Pointers: Concepts, Examples & Practical Applications
- Pointers in C: A Comprehensive Guide to Types, Usage, and Best Practices
- Function Pointers in C: Practical Examples and Best Practices
- Mastering C Pointers: Practical Steps to Advanced Programming
- How to Draft Accurate CNC Drawings: Essential Tips