Double const declaration
Asked Answered
H

8

16

I often see the following function declaration:

some_func(const unsigned char * const buffer)
{

}

Any idea why the const is repeated before the pointer name?

Thanks.

Hatchery answered 28/1, 2010 at 16:56 Comment(5)
It should be const unsigned char* constFlatus
iirc unsigned char const * const also is validBrittney
No, you don't see such declarations "often". The declaration is, in fact, invalid and thus makes no sense. Please, be more careful when posting code with your questions.Kelsiekelso
Reading the declaration from right to left will give you a better understanding. const unsigned char * const is a constant pointer (*const) to an constant unsigned char (const unsigned char).Olympe
Sorry, corrected to right syntax. The right to left reading advice is very useful, thanks! (I actually remembered studying something about the compiler parsing in RTL fashion during CS courses :) )Hatchery
F
31

The first const says that the data pointed to is constant and may not be changed whereas the second const says that the pointer itself may not be changed:

char my_char = 'z';
const char* a = &my_char;
char* const b = &my_char;
const char* const c = &my_char;

a = &other_char; //fine
*a = 'c'; //error
b = &other_char; //error
*b = 'c'; //fine
c = &other_char; //error
*c = 'c'; //error
Flatus answered 28/1, 2010 at 17:1 Comment(3)
+1 But might I suggest adding examples demonstrating that const pointers may not be reassigned. (i.e., char* const x = NULL; x = &some_char;)Imamate
@Imamate I believe I already included that b = &other_char; //errorFlatus
I believe I need to pay more attention :) My apologiesImamate
C
10

type declarations should(?) be read RTL. const modifies the thing on its left, but the rule is complicated by the fact that you can write both const T and T const (they mean the same thing).

  • T * const is a constant pointer to mutable T
  • T & const would be constant reference to mutable T, except references are constant by definition
  • T const * is a mutable pointer to constant T
  • T const & is a reference to constant T
  • T const * const is constant pointer to constant T
Concentre answered 28/1, 2010 at 17:32 Comment(1)
Thanks, the RTL reading advice is useful for such feature cases.Hatchery
A
3

It's a constant pointer to a constant unsigned char. You can't change the pointer nor the thing it points to.

Apostle answered 28/1, 2010 at 16:58 Comment(0)
F
3

In a declaration like const * const T, the first const (before the *) means that what the pointer points at is const (i.e. it's a pointer to a const T). The const after the * means that the pointer itself is const (i.e. can't be modified to point at anything else).

You can read the declaration from the object being declared outward, so const unsigned char * const buffer is read as: "buffer is a const pointer to a const unsigned char" (this is why const should always being placed after what it modifies--with it before, you have to rearrange things to make the sentence--with it declared as unsigned char const * const buffer, translation to English is simple and straighforward (or perhaps "straightbackward", since you actually read from right to left in this case).

Filiate answered 28/1, 2010 at 16:59 Comment(0)
B
2

assuming const unsigned char * const

Everyone is correct that its a const pointer to a const unsigned char.

C++ types read mostly right to left unless there are any modifiers on the far left then these read left to right.

Brittney answered 28/1, 2010 at 17:6 Comment(0)
E
1

This makes it a const pointer to a const value, rather than a mutable pointer to a const value or a const pointer to a mutable value.

Escharotic answered 28/1, 2010 at 16:58 Comment(0)
B
1

const * unsigned char const buffer means that you cannot modify the pointer buffer nor the memory that buffer points to.

Boyette answered 28/1, 2010 at 16:58 Comment(0)
G
1

A couple of articles to help you understand const correctness in C++:

Grouchy answered 28/1, 2010 at 17:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.