3

There's a well-known warning in gcc

warning: passing argument ... from incompatible pointer type [-Wincompatible-pointer-types]

and most often it occurs when trying to do something with parameter argv of function main.

I've read this answer: https://stackoverflow.com/a/28702525/447503 . Ok, the C standard says so and the following code will compile with a warning:

int main(int argc, char *argv[]) {
        const char * const * b = argv;
        return b == 0;
}

Not sure about C++, because g++ compiles same code without warnings.

Is there any reason behind this part of the standard? Can I do something bad after the cast, for example make argv point to non-writable memory?

This may be a duplicate of Why can't I convert 'char**' to a 'const char* const*' in C? , but Aaron's anwer is incorrect: read the comments.

Is Kevin's answer incorrect?

I'm thinking to add a convenience inline function like: const char * const *foo(char * const *a). Unlike C-style casts it would be type safe.

Community
  • 1
  • 1
basin
  • 3,949
  • 2
  • 27
  • 63
  • @DietmarKühl Aaron's anwer is incorrect: read the comments. – basin Dec 07 '15 at 10:07
  • I agree that the linked duplicate had some questionable answers, voting to re-open this in hope that someone can provide a better answer. – Lundin Dec 07 '15 at 10:08
  • 1
    "Is Kevin's answer incorrect?" -- No, that answer is correct. The conversion can be safe (assuming the language's aliasing rule allows it too -- which C++'s does). If it weren't safe, C++ wouldn't have opened this up. @Lundin I think the question should've remained closed, Aaron's answer to that other question indeed misses the point, but the accepted answer there is correct. –  Dec 07 '15 at 10:18
  • The accepted answer in the duplicate is correct. Why ask it again? If you're looking for more details, such as exact quotes from the C and C++ standards, you should state so. – interjay Dec 07 '15 at 10:19
  • I don't believe there's a problem with the C standard, it is very clear. Rather seems GCC doesn't like the pointer to be promoted to qualified pointer-to-type, for reasons unknown. You can rule out all strangeness with constant pointers like this: `typedef const char const_char; const_char** ptr = argv;` and you still get that warning. – Lundin Dec 07 '15 at 10:20
  • C11: `For any qualifier q, a pointer to a non-q-qualified type may be converted to a pointer to the q-qualified version of the type; the values stored in the original and converted pointers shall compare equal.` And that text is all there is, how could it be misinterpreted? How can there be "a problem with the standard"? – Lundin Dec 07 '15 at 10:20
  • It could, because it's human language. If standards were example-driven, there would be much less misunderstanding – basin Dec 07 '15 at 10:24
  • From the C++ side I think my [answer here](http://stackoverflow.com/a/29240053/1708801) is what you are looking for. The logic should be similar for C. – Shafik Yaghmour Dec 07 '15 at 10:24
  • 2
    @Lundin Nobody said anything about misinterpreting. The problem is that the C standard doesn't allow some code which is actually safe, such as that in the question. – interjay Dec 07 '15 at 10:27

0 Answers0