Let me preface this by acknowledging that it's easy enough to avoid this situation by naming types and variables in a way that they don't overlap.
Nevertheless, I'm curious what would happen in the following case:
typedef char jimmypage;
jimmypage *jimmypage;
Would sizeof(jimmypage)
be equivalent to sizeof(char)
or sizeof(char *)
?
Let's make it work:
#include <stdio.h>
typedef char synonym;
int main(void) {
synonym *synonym;
printf("sizeof (synonym) = %ld\n", (long) sizeof (synonym));
return 0;
}
The declaration
synonym *synonym;
declares a variable with the name synonym
of type pointer to type synonym
(equivalent to char
) declared in the surrounding block. This declaration shadows the identifier synonym
declared in the surrounding block, with the effect that from this point forward to the end of the current block, the identifier synonym
will refer to the variable and not the type; that is, in the rest of the block the type name synonym
can no longer be used, because synonym
is a variable.
Supplementary note:
The declaration is legal because the type was declared in a surrounding block. Trying to declare the variable at the same level as the type
typedef char synonym;
synonym *synonym; // Syntax error: identifier redeclared
is a syntax error because the identifier synonym
would be redeclared with a different meaning.
Extra supplementary note:
Two words are synonyms if they have the same meaning. Two words with the same form but different meanings are homonyms.