776 B
776 B
| tags | |
|---|---|
|
A pointer is a reference to the address of a variable in memory.
int x = 27;
int *ptr = &x;
printf("%i\n", x);
// 27
printf("%p\n", *ptr);
// 0x7ffeb44f7eac
The & and * is frankly confusing.
In the previous example, int *ptr = &x, ptr is a variable that holds the
memory address of x. * signals that it is a pointer variable, & is what
does the retrieval.
In the following:
int x = 27;
int *ptr = &x;
int value = *ptr;
We again set ptr to the memory address of x, but we use * on the last line
to de-reference the pointer and get the original value back. Thus value
becomes equal to 27.
Pointers are necessary because C uses a call by value system for function arguments.