Here is a small section of code which has various typedefs for the same structure
The question is, why isn't the const on the first declaration having the desired effect, when all the other const declarations are OK.
It seems like the const cannot get 'inside' the typedef of pnode_t, to make the whole thing a const, whereas making the const part of the typedef itself (as in cnode_t) does the right thing.
--
Code:
#include <stdio.h>
struct node {
struct node *next;
char fname[20];
char lname[20];
int age;
};
typedef struct node node_t; /* typedef a struct */
typedef node_t *pnode_t; /* typedef a non-const pointer to struct */
typedef const node_t *cnode_t;/* typedef a const pointer to struct */
/* vc++.net (and VC6 ) with W1 to W4 warning levels reports */
/* warning C4090: 'initializing' : different 'const' qualifiers */
/* gcc reports the following */
/* tested with gcc (GCC) 3.3.1 (cygming special) */
/* tested with gcc (GCC) 3.2 20020903 (Red Hat Linux 8.0 3.2-7) */
/* warning: initialization discards qualifiers from pointer target type */
void foo ( const void *p ) {
const pnode_t q = p; /* (see NOTE) const applied to a non-const 'pointer' typedef */
cnode_t c = p; /* 'pointer' typedef contains const */
const node_t *r = p; /* const explicitly stated for 'struct' typedef */
const struct node *s = p; /* const explicitly stated for struct */
printf( "%s %s %s %s\n", q->fname, r->fname, s->fname, c->fname );
}
int main(void) {
return 0;
}
It seems like the const cannot get 'inside' the typedef of pnode_t, to make the whole thing a const, whereas making the const part of the typedef itself (as in cnode_t) does the right thing.
--