Mastering C Pointers: From Basics to Advanced Function Passing
This article explains the fundamentals of C pointer variables, covering their memory layout, declaration, initialization, pointer arithmetic with arrays, passing pointers between functions, and using arrays as function parameters, illustrated with clear code examples and diagrams.
1 Pointer Variable Basic Operations
Pointer variables are ordinary variables that occupy a machine word of memory (4 bytes on 32‑bit systems, 8 bytes on 64‑bit systems) and store an address, which is referred to as the pointer's own address.
1.1 Own Address and Own Space
The size of the memory space a pointer occupies equals the machine word length: 32‑bit CPUs use 4 bytes, 64‑bit CPUs use 8 bytes.
1.2 Value, Target Address, and Target Space
The value of a pointer is the address of the memory region it points to; the size of that region is determined by the pointed‑to type.
1.3 Declaration and Initialization
When a pointer is declared without initialization it has its own storage but points to an indeterminate location, making dereferencing as an lvalue illegal. To safely dereference, initialize the pointer by assigning the address of a variable, another initialized pointer of the same type, or the result of a memory‑allocation function.
int a, *iptr, *jptr, *kptr;
iptr = &a;
jptr = iptr;
*jptr = 100;
kptr = NULL; int *ptr;
int *ptr_2;
int a = 1;
ptr_2 = &a;
// *ptr = 0; // illegal, target not set
ptr = &a; // ① assign variable address
ptr = ptr_2; // ② assign initialized pointer
ptr = (int*)malloc(sizeof(int)); // ③ assign malloc result
*ptr = 0; // now legal1.4 Passing Pointer Values Between Functions
Returning a pointer to a local variable leads to undefined behavior because the stack frame is reused after the function returns. Instead, allocate memory on the heap or pass a pointer to a pointer.
#include <stdio.h>
void funcForSpace(int **iptr) {
int a = 10;
*iptr = &a;
}
int main() {
int *pNew;
funcForSpace(&pNew);
printf("%d
", *pNew); // 10, stack still valid
// after stack reuse, value becomes garbage
return 0;
} #include <stdio.h>
#include <malloc.h>
int g(int **iptr) {
if ((*iptr = (int*)malloc(sizeof(int))) == NULL) return -1;
return 0;
}
int main() {
int *jptr;
g(&jptr);
*jptr = 10;
printf("%d
", *jptr); // 10
free(jptr);
return 0;
}2 Pointer Variables and Array Names
In most expressions, an array name decays to a pointer to its first element, enabling pointer arithmetic.
#include <stdio.h>
int main() {
int a[5] = {0};
char b[20] = {0};
*(a+3) = 10; // a+3 points to a[3]
*(b+3) = 'x'; // b+3 points to b[3]
printf("%d, %c
", a[3], b[3]); // 10, x
return 0;
}3 Caller and Callee Pointer Passing
#include <stdio.h>
void swap1(int x, int y) {
int tmp = x; x = y; y = tmp;
}
void swap2(int *x, int *y) {
int tmp = *x; *x = *y; *y = tmp;
}
void caller() {
int a = 10, b = 20;
swap1(a,b);
printf("%d %d
", a, b); // unchanged
swap2(&a, &b);
printf("%d %d
", a, b); // swapped
}
int main() { caller(); return 0; }4 Arrays as Function Parameters
Multidimensional arrays are stored linearly; when passed to a function, the size of all but the first dimension must be specified.
void g(int a[][2]) { // equivalent to void g(int (*a)[2])
a[2][0] = 5;
}
void caller() {
int a[3][2];
int (*p)[2] = a;
*(*(p+2)+0) = 7;
printf("%d
", a[2][0]); // 7
g(a);
printf("%d
", a[2][0]); // 5
}Reference: Kyle Loudon, Mastering Algorithms with C .
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.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
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.
