I’m wondering why C99 allows conversions between incompatible pointer types:
void f(int* p)
{
}
void c(char* p)
{
f(p);
}
Does C11 also allow them? Are conforming implementations required to diagnose such conversions?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
C99 doesn’t permit implicit conversion between pointers of different types (except to/from
void*). here’s what the C99 Rationale says:This is a consequence of the rules for assignment, which has a constraint that one of the following shall hold (when pointers are involved) (C99 6.5.16.1 “Simple assignment”):
Passing a pointer as an argument to a prototyped function follows the same rules because (C99 6.5.2.2/7 “Function calls”):
Both C90 and C11 have similar wording.
I believe that many compilers (including GCC) relax this constraint to issue only a warning because there’s too much legacy code that depends on it. Keep in mind that
void*was an invention of the ANSI C standard, so pre-standard, and probably a lot of post-standard, code generally usedchar*orint*as a ‘generic’ pointer type.