Home > Mobile >  how to understand const and pointer in C expression below?
how to understand const and pointer in C expression below?

Time:11-07

I am reading application code developed in the IBM RSARTE C version. Here is a piece of C code:

const char * const * av = RTMain::argStrings();

How to understand the left-hand side syntax when there are two const and two *?

CodePudding user response:

const char * const * av = RTMain::argStrings();

is the same as

char const * const * av = RTMain::argStrings();

const applies to what's left of const.

So, av is a non-const pointer to a const* to const char.

  • The returned pointer, av, is non-const and can be changed.
  • The pointer av is pointing at is const and can not be changed.
  • The char that pointer is pointing at is const and can not be changed.

CodePudding user response:

Read this declaration

const char * const * av 

from right to left. There is declared the pointer av ( * av ) that points to a constant pointer ( * const ) that in turn points to an object of the type const char.

To simplify this declaration consider this code snippet.

const char *literal = "Hello World!";
const char * const * pointer_to_the pointer_literal = &literal;

So using the pointer pointer_to_the pointer_literal you may not write for example

*pointer_to_the pointer_literal = "Bye";

and you may not write

**pointer_to_the pointer_literal = 'h';
  • Related