C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

cannot convert char** to const char**

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Brad Moore
Guest





PostPosted: Wed Oct 13, 2004 5:13 am    Post subject: cannot convert char** to const char** Reply with quote



Hey all,

I'm getting the following compiler error from my code. I was wondering
if anyone could help me understand the concept behind it (I actually did
try and compile this degenerate example).

int foo(const char* argv[]) { return 0; }

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

Error: cannot convert parameter 1 from char** to const char**


I tried using an explicit type cast, and it worked. However, I'd like
to know the difference between the above code and the following (which
did compile):

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

I've been reading that using const in your args list is a useful way of
noting that an argument isn't changed. If foo(const char* argv[]) is
bad form in some way, please let me know.

To reiterate, I did get the above code to work. I was just wondering
what the good programming practice is (and the concept behind it).

Thanks for your expertise and time,
-Brad
Back to top
Gianni Mariani
Guest





PostPosted: Wed Oct 13, 2004 6:07 am    Post subject: Re: cannot convert char** to const char** Reply with quote



Brad Moore wrote:
Quote:
Hey all,

I'm getting the following compiler error from my code. I was wondering
if anyone could help me understand the concept behind it (I actually did
try and compile this degenerate example).

int foo(const char* argv[]) { return 0; }

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

Error: cannot convert parameter 1 from char** to const char**

You need to consider the meaning of "**" - in this case it means a
pointer to a pointer to a [const] char. This is a double level of
indirection which allows the pointer to be modified in one place and the
object to be modified in another.

If the conversion from char** to const char** was allowed, then you
could write this code.

const char * A[2] = { "A", "B" };
char * B[2];

int main()
{
char ** b = B;
const char ** a = b; // illegal
//const char ** a = ( const char ** ) b; // BAD work-around (1)
//const char * const * a = b; // better option (2)

a[0] = A[0]; // illegal if you use (2) ok if you use (1)

b[0][0] = 'X'; // const violation - big probs if you use (1)
}

This would implicity allow const objects to be accessed in a non-const way.


Quote:


I tried using an explicit type cast, and it worked. However, I'd like
to know the difference between the above code and the following (which
did compile):

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

This is a very different issue, the earlier one had a level of
indirection, this one has none. In this case x is "copied" to a const
value parameter.

Quote:

I've been reading that using const in your args list is a useful way of
noting that an argument isn't changed. If foo(const char* argv[]) is
bad form in some way, please let me know.

To reiterate, I did get the above code to work. I was just wondering
what the good programming practice is (and the concept behind it).

What you did was probably not what you wanted to do.

Back to top
John Harrison
Guest





PostPosted: Wed Oct 13, 2004 6:20 am    Post subject: Re: cannot convert char** to const char** Reply with quote




"Brad Moore" <mooreb (AT) cis (DOT) ohio-state.edu> wrote

Quote:
Hey all,

I'm getting the following compiler error from my code. I was wondering if
anyone could help me understand the concept behind it (I actually did try
and compile this degenerate example).

int foo(const char* argv[]) { return 0; }

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

Error: cannot convert parameter 1 from char** to const char**


I tried using an explicit type cast, and it worked. However, I'd like to
know the difference between the above code and the following (which did
compile):

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

I've been reading that using const in your args list is a useful way of
noting that an argument isn't changed. If foo(const char* argv[]) is bad
form in some way, please let me know.

OK, first thing to note is that foo(char* argv[]) is just another way of
writing foo(char** argv). C and C++ allows you to use array notation in
function parameters but its a lie. The compiler always converts to array to
a pointer. So its better to use the pointer notation, its more truthful.

C++ says its ok to convert char* to const char* because this it harmless.
char* is a pointer to char, and const char* is a pointer to constant char
(note its the char that is constant not the pointer). Adding a const can't
do any harm. So sometimes people think that char** should be convertible to
const char** but this conversion is most definitely not harmless. Here's why

const char a = 'a'; // this is a constant it should never change
char* x;
const char** y = &x; // in reality this is illegal
*y = &a; // now x points to a
*x = 'b'; // now we have modified a

By allowing a conversion from char** to const char** we've managed to write
a sequence of statements which have modified a constant. This is obviously a
bad thing

john





Back to top
Brad Moore
Guest





PostPosted: Wed Oct 13, 2004 3:33 pm    Post subject: Re: cannot convert char** to const char** Reply with quote




I want to thank everyone who responded. I understand the error in my
reasoning.

Thanks,
-Brad
Back to top
Default User
Guest





PostPosted: Wed Oct 13, 2004 4:21 pm    Post subject: Re: cannot convert char** to const char** Reply with quote

John Harrison wrote:


Quote:
OK, first thing to note is that foo(char* argv[]) is just another way
of writing foo(char** argv).

This is true.

Quote:
C and C++ allows you to use array
notation in function parameters but its a lie. The compiler always
converts to array to a pointer. So its better to use the pointer
notation, its more truthful.

This is your personal opinion, not an objective fact. Many people
prefer the other notation, and their programs are none the less for it.



Brian Rodenborn


Back to top
John Harrison
Guest





PostPosted: Wed Oct 13, 2004 6:51 pm    Post subject: Re: cannot convert char** to const char** Reply with quote


"Default User" <first.last (AT) boeing (DOT) com.invalid> wrote

Quote:
John Harrison wrote:


OK, first thing to note is that foo(char* argv[]) is just another way
of writing foo(char** argv).

This is true.

C and C++ allows you to use array
notation in function parameters but its a lie. The compiler always
converts to array to a pointer. So its better to use the pointer
notation, its more truthful.

This is your personal opinion, not an objective fact. Many people
prefer the other notation, and their programs are none the less for it.


OK, well perhaps we can at least agree that people should be aware of the
real meaning of the array notation in a function parameter. Its my
experience in this group that some newbies are not.

John



Back to top
kaikai
Guest





PostPosted: Wed Dec 28, 2005 2:43 pm    Post subject: Re: cannot convert char** to const char** Reply with quote

I know this post is old enough.(Over one year...)
But I do think I have just realize the concept behind it.

'Type' could be converted to 'const Type', as we all know. But to the
situation, 'char **' and 'const char**'. NOTICE, the Type here is not just
'char**'.
'const char**' is NOT 'const Type' with Type be 'char**'. Its 'Type *' with
Type be 'const char*'.
So the problem is actully 'Type1 *' convert to 'Type2 *' where Type1 is
'char*' and Type2 is 'const char*'.
We known that pointer types can not convert from each other unless one of
them is pointer to void (void*). So it is now clear why 'char **' can not
convert to 'const char **'.

Was is a tongue twister ? -_-|||

kaikai


Brad Moore wrotes:

Quote:
Hey all,

I'm getting the following compiler error from my code. I was wondering
if anyone could help me understand the concept behind it (I actually did
try and compile this degenerate example).

int foo(const char* argv[]) { return 0; }

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

Error: cannot convert parameter 1 from char** to const char**


I tried using an explicit type cast, and it worked. However, I'd like
to know the difference between the above code and the following (which
did compile):

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

I've been reading that using const in your args list is a useful way of
noting that an argument isn't changed. If foo(const char* argv[]) is
bad form in some way, please let me know.

To reiterate, I did get the above code to work. I was just wondering
what the good programming practice is (and the concept behind it).

Thanks for your expertise and time,
-Brad



Back to top
deane_gavin@hotmail.com
Guest





PostPosted: Wed Dec 28, 2005 2:51 pm    Post subject: Re: cannot convert char** to const char** Reply with quote


kaikai wrote:
Quote:
I know this post is old enough.(Over one year...)
But I do think I have just realize the concept behind it.

'Type' could be converted to 'const Type', as we all know. But to the
situation, 'char **' and 'const char**'. NOTICE, the Type here is not just
'char**'.
'const char**' is NOT 'const Type' with Type be 'char**'. Its 'Type *' with
Type be 'const char*'.
So the problem is actully 'Type1 *' convert to 'Type2 *' where Type1 is
'char*' and Type2 is 'const char*'.
We known that pointer types can not convert from each other unless one of
them is pointer to void (void*). So it is now clear why 'char **' can not
convert to 'const char **'.

Was is a tongue twister ? -_-|||

kaikai


Brad Moore wrotes:

Hey all,

I'm getting the following compiler error from my code. I was wondering
if anyone could help me understand the concept behind it (I actually did
try and compile this degenerate example).

int foo(const char* argv[]) { return 0; }

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

Error: cannot convert parameter 1 from char** to const char**


I tried using an explicit type cast, and it worked. However, I'd like
to know the difference between the above code and the following (which
did compile):

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

I've been reading that using const in your args list is a useful way of
noting that an argument isn't changed. If foo(const char* argv[]) is
bad form in some way, please let me know.

To reiterate, I did get the above code to work. I was just wondering
what the good programming practice is (and the concept behind it).

Thanks for your expertise and time,
-Brad

http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.17

Gavin Deane


Back to top
kaikai
Guest





PostPosted: Wed Dec 28, 2005 3:03 pm    Post subject: Re: cannot convert char** to const char** Reply with quote

Em..., that example gives us the reason why the convertion is dangerous and
I have read it before. To be honest, I did not understand it until I have
read the example. But I am trying to explain the concept behind it.

kaikai

<deane_gavin (AT) hotmail (DOT) com>
wrote:1135781494.205578.277190 (AT) g43g2000cwa (DOT) googlegroups.com...
Quote:

[snip]

http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.17

Gavin Deane




Back to top
kaikai
Guest





PostPosted: Wed Dec 28, 2005 3:26 pm    Post subject: Re: cannot convert char** to const char** Reply with quote

<deane_gavin (AT) hotmail (DOT) com>
??????:1135781494.205578.277190 (AT) g43g2000cwa (DOT) googlegroups.com...
Quote:

kaikai wrote:
I know this post is old enough.(Over one year...)
But I do think I have just realize the concept behind it.
[snip]
Thanks for your expertise and time,
-Brad

http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.17

Gavin Deane


OMG! I am sorry for my rudeness top-posting.(2 times...)

kaikai



Back to top
Tsubasa
Guest





PostPosted: Thu Dec 29, 2005 4:06 am    Post subject: Re: cannot convert char** to const char** Reply with quote

/***************************************************************
We known that pointer types can not convert from each other unless one
of
them is pointer to void (void*)
***************************************************************/
in fact, we could use reinterpret_cast to convert type1* to type2*, for
instance,

char* ch = new char('');
int** i = NULL;
*i = reinterpret_cast<int*>(ch);

I could compile this code with VC7.1, BCB5.5

maybe C conversion couldn't convert type1* to type2*, but
reinterpret_cast may convert
many strange conversion

all reply will be approciated

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.