This is a newbie C++ question. What is the difference between the following two constructs?
1. const int* const* const x
2. const int**
How do I read these constructs?
You can print a pointer value using printf with the %p format specifier. To do so, you should convert the pointer to type void * first using a cast (see below for void * pointers), although on machines that don't have different representations for different pointer types, this may not be necessary.
The Pointer in C, is a variable that stores address of another variable. A pointer can also be used to refer to another pointer function. A pointer can be incremented/decremented, i.e., to point to the next/ previous memory location. The purpose of pointer is to save memory space and achieve faster execution time.
16) What is the output of C program with structures pointers.? Explanation: F2.
How do I read these constructs?
Read them backwards and read the *
as "pointer to".
const int* const* const
is a constant pointer to a constant pointer to an integer constant.
const int**
is a pointer to a pointer to an integer constant.
It gets a bit easier if you group things the right way. For example, *const
is really one unit meaning "const pointer to" (you can read the const
as a subscript here: *const
). I'd write it as:
const int *const *const p1; // p1 is a const pointer to const pointer to const int
const int **p2; // p2 is a pointer to pointer to const int
Also remember that declarations read "inside out", starting at the identifier being declared.
There is a tool that's useful/fun to decipher declarations: http://cdecl.ridiculousfish.com/
In your case it reports:
const int* const* const x
=> declare x as const pointer to const pointer to const int
const int** x
=> declare x as pointer to pointer to const int
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With