To my understanding, standards are mainly for code portability between compilers, but I'm curious if some things in the standard that are only treated as warnings when not followed can cause software to actually fail, or if code is written that does not comply with the standard, and it compiles/executes where it needs to, can it usually be left alone?
Here's an example from the book Deep C Secrets (explanation about why that happens will be below everything). I'm not sure if this specific issue could cause possible mayhem (though it's an implicit cast to otherwise incompatible types), but it's the example that brought the question to my mind.
void foo (const char ** p) {}
int main (int argc, char ** argv)
{
foo(argv); //compiler says "line 5: warning: argument is incompatible with prototype"
return 0;
}
For the sake of the argument let's assume that the developer says "no big deal" and decides not to fix the warning since the code compiles (on their compiler). If this, hypothetically, could cause software to break why is it not an error? Or is a warning simply brought up because some other compiler may not support the assignment of const char ** = char **
?
why it happens (or just check C FAQ):
On page 92 of this standard, the third possible constraint under (1) for simple assignments is that:
- both operands are pointers to compatible types (with or without qualifications)
- the type pointed to by the left operand has all the qualifiers (or more) than the type pointed to by the right operand
In this case, char **
and const char **
are both pointers to different unqualified types (pointers, one to a const char *
and, a other to a char *
).