C Pointers
Understanding Pointers in C
In C, a pointer is a special type of variable that holds the memory address of another variable rather than the value itself.
Instead of directly storing data, a pointer keeps track of where in memory that data is located, enabling powerful programming techniques.
Fetching an Address Using & (Address-of Operator)
You can retrieve the memory location of any variable using the ampersand (&) symbol.
Example:
int age = 43;
printf("%d\n", age); // Displays the value: 43
Printf("%p\n", &age); // Displays the memory location: e.g., 0x7ffc123abc Here:
- age stores the number 43
- &age returns the memory slot where age lives in RAM
Declaring a Pointer Variable
To define a pointer:
- Use the * symbol before the variable name.
- Match the pointer’s type with the variable it points to.
Example:
int age = 43; Int* pointer = &age;
- int* pointer means this pointer will store the address of an integer.
- pointer = &age assigns the address of age to the pointer.
OutPut:
printf("%p\n", &age); // Shows address of age
Printf("%p\n", pointer); // Also shows address of age (same result) Both display the same location because pointer now tracks where age is stored.
Explanation Recap
- You created an integer named age
- Then declared a pointer pointer that holds the address of age
- Now, pointer refers to the same memory block as age
Dereferencing a Pointer with *
You can extract the actual value stored at the memory address a pointer holds using the * operator again — but this time, for dereferencing.
Example:
int age = 43;
int* pointer = &age;
printf("%p\n", pointer); // Prints address
Printf("%d\n", *pointer); // Prints value at address: 43 Important Note:
- * in declaration: defines a pointer
- * when accessing: fetches the value the pointer refers to
So:
int* ptr; // defines a pointer *ptr // accesses the value stored at ptr’s address
Two Valid Pointer Declarations
Both of the following syntaxes are acceptable:
int* a; Int *b;
Both declare a and b as integer pointers — the positioning of * is a matter of style.
Why Pointers Matter in C
Pointers are a fundamental feature that set C apart from higher-level languages like JavaScript or Python.
Key Benefits:
- Offer direct access to memory
- Enable efficient manipulation of data structures (e.g., linked lists, trees)
- Allow parameter passing by reference
- Crucial for system-level operations, such as memory management and file handling
Use With Caution
While powerful, pointers can be risky:
- Incorrect pointer use can corrupt memory
- Dangling or uninitialized pointers may cause undefined behavior
- Writing to the wrong memory address can crash your program
Summary
| Concept | Description |
|---|---|
| & | Retrieves a variable's memory location |
| * (declaration) | Creates a pointer to a specific data type |
| * (access) | Dereferences the pointer to get the actual value |
| Pointer Type | Must match the variable type it points to |
| Use Cases | Memory access, data structures, performance |
Example Recap
#include <stdio.h>
int main() {
int value = 50;
int* ptr = &value;
printf("Value: %d\n", value); // 50
printf("Address: %p\n", &value); // Address of value
printf("Pointer: %p\n", ptr); // Same address
printf("Dereferenced: %d\n", *ptr); // 50
return 0;
} Prefer Learning by Watching?
Watch these YouTube tutorials to understand C Tutorial visually:
What You'll Learn:
- 📌 Introduction to Pointers in C
- 📌 C_71 Pointers in C - part 1| Introduction to pointers in C | C Programming Tutorials