I often see the following function declaration:
some_func(const unsigned char * const buffer)
{
}
Any idea why the const is repeated before th
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).