Why is the non-const pointer being treated as a const when using typedef?
Asked Answered
H

5

7
typedef char* c;
const c ptr1 = "pointer";
++ptr1; /// error
const char* ptr2 = "pointer";
++ptr2; /// runs fine

Now ptr1 should be of type const char* and thus a non-const pointer, then why is it being treated as a constant pointer ?

Hanahanae answered 8/8, 2012 at 12:53 Comment(1)
Try typedef char* c and typedef const char* const_c, now const_c works like ptr2 and you just need to use const_cinstead of c whenever *c is intended to be const!Grove
P
14

They are not the same.

The first designates a const-pointer-to-char, the second is a pointer-to-const-char.

Try reading right to left:

const char *p;  // p is a pointer to char which is const
char const *p;  // p is a pointer to const char (same as previous)
char * const p; // p is a const pointer to char
char const * const p; // p is a const pointer to const char

By using the typedef typedef char* c you pack the meaning "pointer to char" into a single alias c:

const c p; // p is a const [c] --> p is a const [pointer to char]

Additional explanations:

Typedefs are not in-place-expanded like macros are, i.e.

const c p;

really becomes

const [char*] p;

it does not become

const char* p; // Nope.

Don't expand it like macros in your mind, with the typedefs, you've bound char and * together and formed an atom.

Priapism answered 8/8, 2012 at 12:55 Comment(8)
This is interesting. I'm really surprised. +1Canales
I know that, but when shouldn't it be treated as const char * by the compiler, because c being expanded to char *, that's what I'm asking.Hanahanae
@cirronimbo: See the section "by using the typedef" in my answer.Priapism
I got into the habit of writing type const for this reason, but it seems to drive other programmers (who don't have a good grasp of the language) crazy. I actually think that the standard should rule putting const in front to be obsolescent and eventually outlaw it.Catalogue
@Hanahanae c is not expanded; typedef isn't a textual macro facility. Or, to look at it another way, it implicitly puts parens around the type, so const c is const (char*) rather than (const char)*.Catalogue
@JimBalter: I personally use both forms. I usually choose what is better readable depending on context. Probably this drives even more programmers crazy :)Priapism
@JimBalter_Yeah I know, it was just that I could not find a better word to explain the thing :)Hanahanae
@Hanahanae See my edited comment. The point is that the expansion is structural, not textual, and so the grouping is different.Catalogue
I
3

ptr1 is a const (char *), meaning the pointer itself is a const, whereas ptr2 is a (const char) *, meaning the target of the pointer is const.

Ibnrushd answered 8/8, 2012 at 12:55 Comment(0)
V
0

it has to do with the way that c groups things internally. A typedef isn't like a macro, it doesn't just substitute things in. If you were to pus parenthesis in it would look like this.

const (char*) ptr1 = "pointer";
(const char)* ptr2 = "pointer";
Venepuncture answered 8/8, 2012 at 12:56 Comment(0)
Z
0

Writing it like:

typedef char* c;
c const ptr1 = "pointer";
++ptr1; /// error
char const* ptr2 = "pointer";
++ptr2; /// runs fine

makes the difference more visible, and this should be the same as your example

Zaragoza answered 8/8, 2012 at 13:0 Comment(0)
G
-1

You need a new typedef for the const variable one, like so:

typedef char* c;
typedef const char* const_c;
const_c ptr1 = "pointer";
++ptr1; /// runs fine
const char* ptr2 = "pointer";
++ptr2; /// runs fine

Now ptr1 and ptr2 work just the same way, including implicit casting!

Grove answered 12/5 at 17:50 Comment(4)
This doesn't really address the OP question. With const c ptr1 the const applies to a pointer, but with const char * the const applies to a char.Bays
My comment was an add up to all the others that despite explaining it didn't give a solution.Grove
The solution isn't to create a new typedef, it is to understand how typedefs work. OP wasn't asking how to create a typedef for const char *, they were asking why the application of const to a type alias didn't work as expected.Bays
He didn't want it to work as expected, he just asked why without any other purpose....Grove

© 2022 - 2024 — McMap. All rights reserved.