Skip to contentSkip to conversation

warnkill trivia 2/2

PATCH Mikael Pettersson 2 participants 2 messages Sep 1, 2002

lore
Share & tools
lore
#0 lore raw
Quoted 13 lines · Tomas Szepe

I've been playing a bit with how gcc handles the const qualifiers and made an interesting discovery:

Trying to compile

typedef int *p_int;
void a(const p_int t) { *t = 0; }
void b(const p_int t) { t = (int *) 0; }
void c(const int *t) { *t = 0; }
void d(const int *t) { t = (int *) 0; }
void e(int const *t) { *t = 0; }
void f(int const *t) { t = (int *) 0; }

Quoted 2 lines

will give 'assignment of read-only location' warnings for b(), c() and e(),

In b() t is a const value and you're trying to assign to it, and in c() and e() t is a pointer-to-const and you're trying to assign to *t. The compiler catches this. What's the problem?

Quoted 3 lines

i.e. it's impossible to have a constant pointer to a non-constant value w/o using a qualified typedef.

void g(int * const t) { *t = 0; }

Quoted 2 lines

W/o a typedef, gcc seems unable to tell the difference between 'const int *' and 'int const *' altogether.

There is no difference. Read the C spec, or Harbison&Steele which has had an explanation of 'const' since their '87 2nd Ed.

/Mikael

#1 lore raw
Quoted 4 lines
Quoted 3 lines

i.e. it's impossible to have a constant pointer to a non-constant value w/o using a qualified typedef.

Quoted 8 lines

void g(int * const t) { *t = 0; }

Quoted 2 lines

W/o a typedef, gcc seems unable to tell the difference between 'const int *' and 'int const *' altogether.

There is no difference. Read the C spec, or Harbison&Steele which has had an explanation of 'const' since their '87 2nd Ed.

Ok, that explains it, obviously I (and DaveM :D) didn't know the syntax. Thanks for the reference!

I'm going to redo the patches.

T.