Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

const char * const versus const char *?

Tags:

c++

c

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 is the difference between int * const C and const int * C?

const int * And int const * are the same. const int * const And int const * const are the same. If you ever face confusion in reading such symbols, remember the Spiral rule: Start from the name of the variable and move clockwise to the next pointer or type. Repeat until expression ends.

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

char* p and char *p are exactly equivalent. In many ways, you ought to write char *p since, really, p is a pointer to char . But as the years have ticked by, most folk regard char* as the type for p , so char* p is possibly more common.

What is the difference between constant pointer and pointer constant?

In the constant pointers to constants, the data pointed to by the pointer is constant and cannot be changed. The pointer itself is constant and cannot change and point somewhere else.


The latter prevents you from modifying the_string inside print_string. It would actually be appropriate here, but perhaps the verbosity put off the developer.

char* the_string : I can change which char the_string points to, and I can modify the char to which it points.

const char* the_string : I can change which char the_string points to, but I cannot modify the char to which it points.

char* const the_string : I cannot change which char the_string points to, but I can modify the char to which it points.

const char* const the_string : I cannot change which char the_string points to, nor can I modify the char to which it points.


  1. Mutable pointer to a mutable character

    char *p;
    
  2. Mutable pointer to a constant character

    const char *p;
    
  3. Constant pointer to a mutable character

    char * const p; 
    
  4. Constant pointer to a constant character

    const char * const p;
    

const char * const means pointer as well as the data the pointer pointed to, are both const!

const char * means only the data the pointer pointed to, is const. pointer itself however is not const.

Example.

const char *p = "Nawaz";
p[2] = 'S'; //error, changing the const data!
p="Sarfaraz"; //okay, changing the non-const pointer. 

const char * const p = "Nawaz";
p[2] = 'S'; //error, changing the const data!
p="Sarfaraz"; //error, changing the const pointer. 

(I know this is old but I wanted to share anyway.)

Just wanted to elaborate on Thomas Matthews' answer. The Right-Left Rule of C type declarations pretty much says: when reading a C type declaration start at the identifier and go right when you can and left when you can't.

This is best explained with a couple examples:

Example 1

  • Start at the identifier, we can't go right so we go left

    const char* const foo
                ^^^^^
    

    foo is a constant...

  • Continue left

    const char* const foo
              ^
    

    foo is a constant pointer to...

  • Continue left

    const char* const foo
          ^^^^
    

    foo is a constant pointer to char...

  • Continue left

    const char* const foo
    ^^^^^
    

    foo is a constant pointer to char constant (Complete!)

Example 2

  • Start at the identifier, we can't go right so we go left

    char* const foo
          ^^^^^
    

    foo is a constant...

  • Continue left

    char* const foo
        ^
    

    foo is a constant pointer to...

  • Continue left

    char* const foo
    ^^^^
    

    foo is a constant pointer to char (Complete!)

Example 1337

  • Start at the identifier, but now we can go right!

    const char* const* (*foo[8])()
                            ^^^
    

    foo is an array of 8...

  • Hit parenthesis so can't go right anymore, go left

    const char* const* (*foo[8])()
                        ^
    

    foo is an array of 8 pointer to...

  • Finished inside parenthesis, can now go right

    const char* const* (*foo[8])()
                                ^^
    

    foo is an array of 8 pointer to function that returns...

  • Nothing more to the right, go left

    const char* const* (*foo[8])()
                     ^
    

    foo is an array of 8 pointer to function that returns a pointer to a...

  • Continue left

    const char* const* (*foo[8])()
                ^^^^^
    

    foo is an array of 8 pointer to functions that returns a pointer to a constant...

  • Continue left

    const char* const* (*foo[8])()
              ^
    

    foo is an array of 8 pointer to functions that returns a pointer to a constant pointer to a...

  • Continue left

    const char* const* (*foo[8])()
          ^^^^
    

    foo is an array of 8 pointer to functions that returns a pointer to a constant pointer to a char...

  • Continue left

    const char* const* (*foo[8])()
    ^^^^^
    

    foo is an array of 8 pointer to functions that returns a pointer to a constant pointer to a char constant (Complete!)

Further explanation: http://www.unixwiz.net/techtips/reading-cdecl.html


Many people suggest reading the type specifier from right to left.

const char * // Pointer to a `char` that is constant, it can't be changed.
const char * const // A const pointer to const data.

In both forms, the pointer is pointing to constant or read-only data.

In the second form, the pointer cannot be changed; the pointer will always point to the same place.