const - are these 2 the same?

const char *p = a; // changeable pointer to constant char
char const *p = a; // changeable pointer to constant char

I'm taking an assessment test. For some reason I'm confused by the second one. My guess is it's "changeable pointer to constant char" but I want to get 100% on this assessment so I can possibly get a job, and I want to double-check it.
They are the same type.

Tip: read pointer declarations from right to left.
1
2
3
const char* p1 = nullptr ; // p1 (*) is a pointer to const char
char const* p2 = nullptr ; // p2 (*) is a pointer to const char
char* const p3 = nullptr ; // p3 is a const (*) pointer to char 

To be sure I suggest to read a declaration from the right to the left starting from * So the declaration

const char *

means

* - ponter to
char - character
const - which is const

This declaration

char const *

means the same

* - pointer to
const - const
char - character.

And this declaration

const char * const

means

const - const
* - pointer to
char - character
const - which is const

So as the result you will get const pointer to const char
Last edited on
closed account (zb0S216C)
Here's my version:

1
2
3
4
const char *String;        // A 
char const *String;        // B
char * const String;       // C
const char * const String; // D 

A & B) A pointer to constant data. This pointer can point to different locations, but cannot modify the data it points to.

C) A constant pointer to non-constant data. This pointer cannot point to another location, but can modify the data it points to.

D) A constant pointer to constant data. This pointer cannot point to another location, nor can it change the data it points to.

Wazzak
Last edited on
Topic archived. No new replies allowed.