✨Organizing C Pointer Declarations
1. Common Declarations Link to heading
-
int a
: Declaresa
as an integer variable. -
int const a
: Declares a constant integera
(cannot be modified). -
int *a
: Can be understood as “*a
is an integer”, soa
is a pointer to an integer. -
int **a
: Since “**a
is an integer”,a
is a pointer to a pointer to an integer.
Note:
int *a, b;
declaresa
as a pointer to an integer, butb
is just an integer—not a pointer.
-
int const *p
:p
is a pointer to a constant integer (the value pointed to cannot be modified). -
int *const p
: Declaresp
as a constant pointer to an integer (the pointer itself cannot change, but the value pointed to can be modified).
2. Combined with Function Declarations Link to heading
-
int f()
: Declares a functionf
that returns an integer. (This is the old-style declaration without parameter types.) -
int *f()
: Sincef()
has higher precedence, this declaresf
as a function returning a pointer to an integer. -
int (*f)()
:*f
binds first, meaningf
is a pointer to a function that returns an integer. -
int *(*f)()
: Declaresf
as a pointer to a function that returns a pointer to an integer.
3. Combined with Arrays Link to heading
-
int a[]
: Declares an array of integers. -
int *a[]
: Since the subscript operator[]
has higher precedence, this declaresa
as an array of pointers to integers. -
int (*a[])()
:a
is an array of function pointers, where each pointer points to a function returning an integer.