warnkill trivia 2/2
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;
voida(const p_int t){ *t = 0; }
voidb(const p_int t){ t = (int *) 0; }
voidc(const int *t){ *t = 0; }
voidd(const int *t){ t = (int *) 0; }
voide(int const *t){ *t = 0; }
voidf(int const *t){ t = (int *) 0; }
Quoted 2 lines
will give 'assignment of read-only location' warnings for
b(),c()ande(),
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
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.