Pointers: addresses and dereferencing
Pointers are the idea that makes C powerful and, for many, hard. But the concept is simple: every variable lives somewhere in memory and that place has an address. A pointer is a variable that stores an address. With it you can reach another variable without using its name, share it with a function or walk through a region of memory. Spend time on this lesson: almost everything that follows depends on it.
Two operators: & and *
The & operator applied to a variable gives its address. A pointer to int is declared with int *p. The * operator applied to a pointer dereferences it: it accesses the value stored at the address. So, if p points to x, reading *p is the same as reading x, and writing *p = 10 changes x. An address is printed with %p, converting the pointer to void *. Notice that the asterisk means two things depending on context: in a declaration it says it is a pointer; in an expression, it means follow the address.
#include <stdio.h>
int main(void) {
int x = 42;
int *p = &x; /* p holds the address of x */
printf("x = %d\n", x);
printf("address of x = %p\n", (void *)&x);
printf("p = %p\n", (void *)p);
printf("*p = %d\n", *p); /* 42 */
*p = 100; /* writes through the pointer */
printf("x = %d\n", x); /* 100 */
int **pp = &p; /* pointer to a pointer */
printf("**pp = %d\n", **pp);
return 0;
}NULL and uninitialized pointers
An uninitialized pointer contains a random address; dereferencing it is undefined behavior and usually ends in a segmentation fault, the most typical memory error. To indicate that a pointer points to nothing use NULL (or nullptr in C23). Dereferencing NULL also fails, but predictably, and you can check beforehand: if (p != NULL). Always initialize your pointers, with a valid address or with NULL.
#include <stdio.h>
static void print_if_valid(const int *p) {
if (p == NULL) {
printf("null pointer, nothing to read\n");
return;
}
printf("value = %d\n", *p);
}
int main(void) {
int n = 7;
int *good = &n;
int *none = NULL;
print_if_valid(good);
print_if_valid(none);
/* int *bad; *bad = 1; undefined behavior: bad was never set */
return 0;
}Pointers and types
The pointer type matters: it tells how many bytes are read when dereferencing and how to interpret them. An int * reads four bytes as an integer; a char * reads one. That is why you should not assign a pointer of one type to another without a clear reason. The special type void * is a generic pointer that can hold the address of any data, but it cannot be dereferenced directly: it must be converted to the right type first. It is the one used by functions such as malloc.
#include <stdio.h>
int main(void) {
int a = 1, b = 2;
const int *reader = &a; /* cannot write through reader */
reader = &b; /* but it can point elsewhere */
printf("reader sees %d\n", *reader);
int *const fixed = &a; /* always points to a */
*fixed = 50; /* but it can change a */
printf("a = %d\n", a);
printf("sizeof(int *) = %zu, sizeof(char *) = %zu\n",
sizeof(int *), sizeof(char *));
return 0;
}✎ Meine Notizen
Nur in diesem Browser gespeichert. Melde dich an, um es im Konto zu behalten.0/5000 Zeichen