Re: cannot convert char** to const char**

Discussion in 'C++' started by kaikai, Dec 28, 2005.

  1. kaikai

    kaikai Guest

    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
    kaikai, Dec 28, 2005
    #1
    1. Advertising

  2. kaikai

    Guest

    kaikai wrote:
    > 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
    , Dec 28, 2005
    #2
    1. Advertising

  3. kaikai

    kaikai Guest

    kaikai, Dec 28, 2005
    #3
  4. kaikai

    kaikai Guest

    <>
    ??????:...
    >
    > 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
    kaikai, Dec 28, 2005
    #4
  5. kaikai

    Tsubasa Guest

    /***************************************************************
    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('\0');
    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
    Tsubasa, Dec 29, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brad Moore
    Replies:
    5
    Views:
    5,938
    John Harrison
    Oct 13, 2004
  2. Abhijit Bhadra
    Replies:
    2
    Views:
    7,417
    Ron Natalie
    Dec 1, 2004
  3. Replies:
    24
    Views:
    800
    Netocrat
    Oct 30, 2005
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    996
    Ian Collins
    May 9, 2006
  5. Replies:
    3
    Views:
    5,597
    Frederick Gotham
    Aug 17, 2006
Loading...

Share This Page