c++ – What does const * mean?

Question:

It is with a star – demonic constructions can take the form

const char* const* blah_blah ,

char const* blah

int const* const integer ,

int* const* const and so on in a wide variety of combinations. How to decipher these styles and why would anyone even need to wrap themselves in const like a silkworm?

Answer:

These entries are easiest to view from right to left. For instance,

This announcement

const char * const  * blah_blah;

declares a pointer named blah_blah that points to a constant pointer (since the const qualifier precedes * when viewed from right to left) that points to a constant object of type char .

Here is a simple program example to make this declaration easier to understand.

#include <stdio.h>

int main(void) 
{
    const char * const  * blah_blah;
    const char *p = "Hello";
    blah_blah = &p;
    puts( *blah_blah );
}

The console will display

Hello

You can enter typedef names for clarity

typedef const char CONST_CHAR_TYPE;
typedef CONST_CHAR_TYPE *POINTER_TO_CONST_CHAR_TYPE;

const POINTER_TO_CONST_CHAR_TYPE *blah_blah;

in this case, the blah_blah variable is a pointer to a constant pointer to a constant char object.

I think it will be easier to understand these declarations if you follow the grammar of the pointer definition.

A pointer in the C standard is defined as follows

pointer: 
    * type-qualifier-listopt 
    * type-qualifier-listopt pointer

that is, the * sign may be followed by a list of qualifications that apply to it.

Therefore, the previous declaration can be written as

const char ( * const ( *blah_blah ) );

You can make the blah_blah pointer blah_blah constant. But then it must be explicitly initialized on declaration if it has automatic memory duration. for instance

const char ( * const ( * const blah_blah ) ) = /* некоторое значение */;

or

const char * const * const blah_blah  = /* некоторое значение */;

Here is a demo in which the blah_blah pointer blah_blah also declared as constant.

#include <stdio.h>

int main(void) 
{
    const char *p = "Hello";
    const char ( * const ( * const  blah_blah ) ) = &p;
    puts( *blah_blah );
}

The declaration of this pointer is read as a constant pointer to a constant pointer to a constant object of type char .

You can omit the parameter identifier in the declarations of functions that have a prototype.

Below is such a program

#include <stdio.h>

void display( const char ( * const ( * const ) ) );
void display( const char * const * const );
void display( const char ( * const  ( * ) ) );
void display( const char * const  * );

int main(void) 
{
    const char *p = "Hello";
    display( &p );
}

void display( const char * const * const blah_blah )
{
    puts( *blah_blah );
}

Since there can be several declarations of the same function (without its definition), all of the above functions declare the same function. The so-called top-level qualifier, which refers directly to a parameter, can be removed from the function declaration. That is (another example) these function pairs declare the same functions

void f( const int x );
void f( int x );

and

void g( int * const p );
void g( int * p );
Scroll to Top