Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ : Meaning of const char*const*

Tags:

In one of the C++ programs, I saw a function prototype : int Classifier::command(int argc, const char*const* argv)

What does const char*const* argv mean? Is it the same as const char* argv[]? Does const char** argv also mean the same?

like image 602
vigs1990 Avatar asked Apr 05 '12 13:04

vigs1990


People also ask

What is the difference between const char * and char * const?

The difference is that const char * is a pointer to a const char , while char * const is a constant pointer to a char . The first, the value being pointed to can't be changed but the pointer can be. The second, the value being pointed at can change but the pointer can't (similar to a reference).

What does const char mean?

const char* is a mutable pointer to an immutable character/string. You cannot change the contents of the location(s) this pointer points to. Also, compilers are required to give error messages when you try to do so. For the same reason, conversion from const char * to char* is deprecated.

What does char * mean in C?

In C, char* means a pointer to a character. Strings are an array of characters eliminated by the null character in C.

What is const int * const?

const int* const is a constant pointer to constant integer This means that the variable being declared is a constant pointer pointing to a constant integer. Effectively, this implies that a constant pointer is pointing to a constant value.


2 Answers

From the C++ Super-FAQ:

Read the pointer declarations right-to-left.

  • const X* p means "p points to an X that is const": the X object can't be changed via p.
  • X* const p means "p is a const pointer to an X that is non-const": you can't change the pointer p itself, but you can change the X object via p.
  • const X* const p means "p is a const pointer to an X that is const": you can't change the pointer p itself, nor can you change the X object via p.

And, oh yea, did I mention to read your pointer declarations right-to-left?

const char * const * is the same as char const * const *: a (non-const) pointer to a const pointer to a const char.

const char * is the same as char const *: a (non-const) pointer to a const char.

const char * * is the same as char const * *: a (non-const) pointer to a (non-const) pointer to a const char.

like image 163
Samuel Harmer Avatar answered Oct 17 '22 03:10

Samuel Harmer


No, it's not the same as const char *argv[]. The const prohibits modifications of the dereferenced value at the particular level of dereferencing:

**argv = x; // not allowed because of the first const *argv = y; // not allowed because of the second const argv = z; // allowed because no const appears right next to the argv identifier 
like image 34
Blagovest Buyukliev Avatar answered Oct 17 '22 03:10

Blagovest Buyukliev