Master C Pointers: From Memory Basics to Advanced Usage
This article explains C pointers in depth, covering memory layout, declaration, initialization, dereferencing, pointer arithmetic, function pointers, passing by reference, dynamic memory allocation, and common use cases, with clear code examples for each concept.
Memory and Addresses
RAM stores program data and instructions as a linear sequence of bytes, each identified by a unique address. When a variable is declared in C, the compiler reserves a memory location for it, analogous to assigning a house a unique street address.
Example:
int x = 5;</code>
<code>int y = 10;On a typical 32‑bit system the two integers might occupy the following addresses (each int occupies 4 bytes):
Address 1000 – stores the value 5 (variable x)
Address 1004 – stores the value 10 (variable y)
Understanding these addresses is the foundation for using pointers, which hold memory addresses and enable direct access to the data stored at those locations.
Declaring and Initializing Pointers in C
Declaration
A pointer is declared by specifying the type it points to, followed by an asterisk ( *) and the pointer name: int *ptr; This creates a pointer ptr capable of storing the address of an int variable.
Initialization
After declaration, the pointer should be initialized with the address of an existing variable using the address‑of operator ( &).
int x = 5;</code>
<code>int *ptr = &x; // ptr now holds the address of x (e.g., 1000)Dereferencing Pointers
Dereferencing retrieves the value stored at the address a pointer references, using the asterisk ( *) operator:
int x = 10;</code>
<code>int *ptr = &x; // ptr points to x</code>
<code>int value = *ptr; // value becomes 10After dereferencing, value contains the integer stored at the memory location pointed to by ptr.
Pointer Arithmetic
Pointer arithmetic allows a pointer to move through contiguous memory, which is essential for traversing arrays and strings.
Example with an integer array:
int numbers[] = {10, 20, 30};</code>
<code>int *ptr = numbers; // points to numbers[0]</code>
<code>printf("%d
", *ptr); // prints 10</code>
<code>ptr += 2; // now points to numbers[2]</code>
<code>printf("%d
", *ptr); // prints 30</code>
<code>ptr--; // back to numbers[1]</code>
<code>printf("%d
", *ptr); // prints 20This technique is vital for navigating arrays and for handling dynamically allocated memory blocks.
Pointers and Functions in C
Function Pointers
Function pointers store the address of a function, enabling callbacks and dynamic function selection.
int (*operation)(int, int); // declaration</code>
<code>operation = add; // point to a function named add</code>
<code>int result = operation(5, 3); // invoke add via the pointerPassing by Reference
By passing a pointer to a function, the function can modify the original variable.
void modifyValue(int *x) { *x = 42; }</code>
<code>int a = 10;</code>
<code>modifyValue(&a); // a becomes 42Dynamic Memory Allocation
Functions can allocate memory on the heap and return a pointer to the newly allocated block.
int *createArray(int size) {</code>
<code> int *arr = (int *)malloc(size * sizeof(int));</code>
<code> return arr; // caller must later free(arr)</code>
<code>}Common Uses of Pointers
Dynamic memory allocation (heap management)
Array and string manipulation
Passing arguments by reference to allow functions to modify callers' data
Implementing data structures such as linked lists, trees, and graphs
Resource management (e.g., file handles, sockets)
Practicing Pointers
Proficiency with pointers improves memory management, enables low‑level data manipulation, and is essential for writing efficient C programs. Working through the examples above and experimenting with additional scenarios (e.g., pointer to struct, pointer arrays, double pointers) will deepen understanding and help avoid common pitfalls such as dereferencing null or dangling pointers.
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.
