function parameter doubt

I was going through the C book Peter van der Linden and it explained that the following code is wrong ...

1
2
3
4
5
6
7
8
9
10

 void foo( char** argv){
    printf("cool");
}
int main(int argc, const char** argv)
{
    
    foo(argv);
    
}


but it is compiling normally and running? Is it now a valid piece of code or is it my compiler extensions that are doing this….just wanted to confirm..

thanks !
This shouldn't compile cleanly, the second parameter in main should be "char **" not "const char **"
Can you say which compiler and version you're using and any flags you have set?
foo is not using argv... and main is not returning( like void main() )... main's argv is const and you are passing it to foo... which parameter is not const...
i am using llvm 5.1 , i have not enabled any flags ...
It is working under a c file but not a c++ file ... but the original example was presented not to work under c!
it explained that the following code is wrong ...
Did it tell, why?

but it is compiling normally and running?
Compilers can use some extensions or allow some Standard violations.

const char** argv should be char** argv

const char**char** conversion should not be allowed.

You omit a return statement from main() it was allowed beginning fron C99. If your book uses older standard, you should return 0 at the end of main.
sorry for the late reply been a bit busy :

The book said :
"The argument passing should behave like an assignment "

argument char *s matches parameter const char *s but char **argv does not match const char **arg

ONE OF THE FOLLOWING SHALL HOLD TRUE:

"Both operands are pointers to qualified unqualified versions of compatible types ,and the type pointed to by the left has all the qualifiers of the type pointed to by the right"


can somebody explain what the 2 statements mean ??what is qualified type and what is unqualified type ?
what is qualified type and what is unqualified type ?
qualified type is type to which one or more cv-qualifiers are applied:
1
2
char ← unqualified type
const char ← qualified type.

For some reasons you have quote for assigment, not initialization (which happens in your example, twice) without explaining why it is relevant:
the same type constraints and conversions as for simple assignment apply, taking the type of the scalar to be the unqualified version of its declared type.


In your case types are: pointer to (pointer to const char) and pointer to (pointer to char). As pointers to diffrent types are not compatible, your initialization fails.

http://www.thecodingforums.com/threads/re-const-char-and-char-incompatibility.967267/
Last edited on

The second assignment fails the first test. The pointed-to types are
const char * and char * and these are not qualified or unqualified
versions of compatible types. A const-qualified pointer has the const
in a different place: char * const. In const char * the const is
qualifying the char, not the pointer type.


why are they not qualified or unqualified version of compatible types ??
const in a different place ..??
Last edited on
const in a different place ..??
... means differenct things:
const char* or char const * — pointer to constant character (you can change pointer, but not the value it points too)
char* const — constant pointer to character (you cannot change pointer, but can change freely values it points to)
const char* const — constant pointer to constant character.

why are they not qualified or unqualified version of compatible types ??
Because types pointer to char and pointer to constant char are not compatible. Pointers to different types are not compatible.
Both initializer and initialized types are unqualified, so first part is irrelevant.

This might help: http://c-faq.com/ansi/constmismatch.html
Topic archived. No new replies allowed.