Goal of the Docs

The pointer in C is powerful but confusing. We can understand pointer better if we think it logically and use it concisely. This document is trying to bridge the gap between logic thinking and concise usage.

Target of a Pointer

The target of a pointer could be NULL, a normal variable, a pointer variable, a function, an array, a structure, or a string. The target must be defined first, assign its address to a pointer, then use the pointer to indirectly access the target.

Dimensional Analysis

Pointers are used to store memory address. Dereferencing pointers can access memory content including data, address, and code.

Description

Memory Address and Content

int v; // v is an integer variable
int a[5]; // a[] is an array with 5 integer variables a[0], a[1], ..., a[4]
int *p; // p is an integer pointer
int f(); // f() is a function which returns an integer

Assign a memory address to a pointer.

Dereference a pointer to access data.

Examples

1. Pointer to Data

data_type v, *pv;
pv = &v; // v is a variable, &v is its address

data_type a[5], *pa;
pa = &a[0] // think logically: a[0] is a variable, &a[0] is its address
pa = a // use concisely: a is array name and a = &a[0] 

address
data

2. Pointer to Function

data_type fd(), (*pfd)();
pfd = &fd; // thinking
pfd = fd; // usage

data_type *fa(), *(*pfa)();
pfa = &fa;
pfa = fa;
address
data