Does that mean in int *x
x is a pointer to the type int or it's a pointer to an integer value?
x
is an object stores values of type int *
; that is, it stores the address of an int
object.
It gives the value 16.
What does it mean?
It means you've invoked undefined behavior - you're using the wrong format specifier for the type.
The proper way to print a pointer value is
printf( "%p\n", (void *) x );
In the declaration
int *x;
the initial value of x
is indeterminate - it could be anything, from 0x00000000
to 0x00000010
(16
) to 0xDEADBEEF
to anything else.
There's nothing magic about pointer variables - they store values of a specific type, like an int
variable stores integer values and double
variable stores floating-point values.
Pointer declaration syntax and operations on pointers are a little non-intuitive and hard to grasp at first, but pointer values themselves are relatively simple things to understand; they're just addresses1 of objects (or functions) in memory.
There's no single pointer type - an int *
is a distinct type from a double *
, which is a distinct type from a char *
, etc. Different pointer types may have different sizes or representations, but on platforms like x86 they all have the same representation.
In a declaration, the presence of a unary *
in the declarator means the variable has pointer type:
T *p; // p is a pointer to T
T *ap[N]; // ap is an array of pointers to T
T (*pa)[N]; // pa is a pointer to an array of T
T *fp(); // fp is a function returning a value of type pointer to T
T (*pf)(); // pf is a pointer to a function returning a value of type T
T **pp; // pp is a pointer to pointer to T - it stores the address of
// an object of type T *
In an expression, the presence of the unary *
operator means we want to dereference the pointer and obtain the value of the thing it points to:
int x = 10;
int *p = &x; // save the address of x in p
printf( "%d\n", *p ); // print the value stored in x by dereferencing p
- More properly, they're abstractions of addresses. Whether those addresses are physical or virtual depends on the environment you're operating in.