Master C Pointers: From Basics to Multi‑Level and Array Techniques
This guide explains C pointers, covering their definition, basic syntax, pointer arithmetic, multi‑level pointers, the relationship between pointers and multi‑dimensional arrays, and the distinction between array pointers and pointer arrays, supplemented with clear code examples and visual analogies.
1. Introduction to Pointers
In C, a pointer stores the memory address of another variable, enabling low‑level hardware manipulation. Declaring a variable (e.g., int a = 1;) allocates space in RAM; the address of that space can be stored in a pointer variable.
2. Basic Pointer Syntax
Appending an asterisk after a type creates a pointer type ( int *p;). The address‑of operator & yields the address of a variable, which can be assigned to a pointer.
int a = 1; // int variable</code>
<code>int *p = &a; // p holds the address of a</code>
<code>printf("%p
", &a); // prints address of a</code>
<code>printf("%p
", p); // prints the same addressDereferencing with *p retrieves the value stored at that address.
3. Pointer Arithmetic
Adding or subtracting an integer to a pointer moves it by the size of the pointed‑to type. For example, with a char array:
char a[5] = {1,2,3,4,5};</code>
<code>char *p = a;</code>
<code>printf("%d
", *p); // a[0]</code>
<code>printf("%d
", *(p+1)); // a[1]Thus p+1 points to the next element, not merely adds 1 to the stored value.
4. Multi‑level Pointers
A pointer can itself be pointed to, forming second‑level (or higher) pointers. The analogy of nested lockers illustrates this: a locker number is an address, a pointer is a locker containing another locker’s number, and a double pointer stores the address of the first pointer.
int a = 1;</code>
<code>int *p = &a;</code>
<code>int **pp = &p; // second‑level pointer</code>
<code>int ***ppp = &pp; // third‑level pointer5. Multi‑dimensional Arrays and Pointers
A two‑dimensional array int a[3][2] can be treated as an array of pointers. Accessing elements can be expressed with pointer arithmetic:
int a[3][2] = {{1,2},{3,4},{5,6}};</code>
<code>printf("%d
", (*a)[0]); // a[0][0]</code>
<code>printf("%d
", (*(a+1))[0]); // a[1][0]</code>
<code>printf("%d
", *(*(a+2)+1)); // a[2][1]In general, a[m][n] == *(*(a + m) + n).
6. Array Pointers vs Pointer Arrays
An array pointer is a pointer that points to an entire array, e.g., int (*ap)[2] = &a;. A pointer array is an array whose elements are pointers, e.g., char *colors[3] = {"red","green","blue"};. The placement of brackets and asterisks determines which form you have; parentheses clarify precedence.
char *a[3] = {"red","green","blue"};</code>
<code>char **pp = a; // pp points to the first element of the pointer array7. Key Takeaways
Pointers are variables that store addresses and themselves occupy memory.
The size of a pointer depends on the architecture (4 bytes on 32‑bit, 8 bytes on 64‑bit) and is unrelated to the pointed‑to type.
Understanding pointer arithmetic, multi‑level pointers, and the relationship between arrays and pointers is essential for low‑level C programming.
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.
