Master C Pointers: From Memory Basics to Advanced Techniques
This tutorial explains C pointers in depth, covering memory addresses, declaration, initialization, dereferencing, pointer arithmetic, function pointers, passing by reference, and dynamic memory allocation, with clear code examples and practical usage tips.
Memory and Addresses
Memory (RAM) stores program data and instructions as bytes, each with a unique address. Declaring a variable in C reserves a memory location, similar to assigning a house with an address. For example, two integers int x = 5; and int y = 10; might occupy addresses 1000 and 1004 respectively.
Address Data
1000 5
1004 10Understanding these addresses is essential for using pointers, which store memory addresses.
Declaring and Initializing Pointers
Declaration
To declare a pointer, specify the data type followed by an asterisk and the pointer name, e.g., int *ptr;. This creates a variable that can hold the address of an integer.
Initialization
After declaration, assign the address of a variable using the & operator:
int x = 5; int *ptr = &x;. Now ptr holds the address 1000, the location of x.
Variable Address Value
x 1000 5
ptr ---- 1000Dereferencing Pointers
Dereferencing accesses the value stored at the address a pointer points to, using the * operator. Example:
int x = 10;
int *ptr = &x; // ptr points to x
int value = *ptr; // value becomes 10The variable value now contains the integer 10 retrieved via ptr.
Pointer Arithmetic
Pointer arithmetic lets you move a pointer through memory, which is useful for arrays and strings. Example with an integer array:
int numbers[] = {10, 20, 30};
int *ptr = numbers; // points to numbers[0]
printf("%d
", *ptr); // prints 10
ptr += 2; // now points to numbers[2]
printf("%d
", *ptr); // prints 30
ptr--;
printf("%d
", *ptr); // prints 20This technique enables efficient navigation of contiguous memory structures.
Pointers and Functions
Function Pointers
Function pointers store the address of a function, allowing dynamic calls and callbacks.
int (*operation)(int, int); // declare function pointer
operation = add; // point to function add
int result = operation(5, 3); // call add via pointerPassing by Reference
Passing a pointer to a function lets the function modify the original variable:
void modifyValue(int *x) {
*x = 42; // change the caller's variable
}Calling modifyValue(&var); sets var to 42.
Dynamic Memory Allocation
Functions can allocate memory on the heap and return a pointer to it:
int *createArray(int size) {
int *arr = (int *)malloc(size * sizeof(int));
return arr;
}The caller receives a pointer to a newly allocated integer array.
Common Uses of Pointers
Dynamic memory allocation
Array manipulation
Passing arguments by reference
Implementing data structures
Resource management
Practicing Pointer Skills
Mastering pointers enhances your ability to manage memory efficiently, manipulate data structures, and write high‑performance C programs. Regular practice with the examples above will deepen your understanding and improve your coding proficiency.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
