Trouble understanding pointer to a const array.

Discussion in 'C++' started by Rob, Mar 7, 2004.

  1. Rob

    Rob Guest

    Hi,

    I receive the following compile error.

    c:\..\Blah.cpp(52) : error C2440: 'initializing' : cannot convert from
    'const char *const [64]' to 'char ** '
    Conversion loses qualifiers


    < outside of main>

    enum { sz = 64};
    const char* const names[sz] =
    { "Frodo", "Gollum", "Sam", "Treebeard",
    "King", "Garcia", "Burdine",
    "Eriksen", NULL };

    <within main>
    <line 52> for (char** p = names; *p; p++)
    {;}


    I thought names was a pointer to an array of string literals, whose
    values could not be change, nor could the location where names pointed
    to be changed.

    With char** p, I'm trying to point to where names points to. Please
    explain where I've gone wrong.

    Thanks,

    Rob
    Rob, Mar 7, 2004
    #1
    1. Advertising

  2. "Rob" <> wrote...
    > I receive the following compile error.
    >
    > c:\..\Blah.cpp(52) : error C2440: 'initializing' : cannot convert from
    > 'const char *const [64]' to 'char ** '
    > Conversion loses qualifiers
    >
    >
    > < outside of main>
    >
    > enum { sz = 64};
    > const char* const names[sz] =


    If you have your "list" of names always end with a null pointer, why
    have the size at all?

    > { "Frodo", "Gollum", "Sam", "Treebeard",
    > "King", "Garcia", "Burdine",
    > "Eriksen", NULL };
    >
    > <within main>
    > <line 52> for (char** p = names; *p; p++)


    If you need to iterate over the array of const char*, you have to
    declare your 'p' as const char**.

    > {;}
    >
    >
    > I thought names was a pointer to an array of string literals, whose
    > values could not be change, nor could the location where names pointed
    > to be changed.
    >
    > With char** p, I'm trying to point to where names points to. Please
    > explain where I've gone wrong.


    One more thing you've done wrong is not posting the _entire_ code.
    Consider visiting the FAQ section 5 at some point in the future.

    Victor
    Victor Bazarov, Mar 7, 2004
    #2
    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. coala
    Replies:
    3
    Views:
    363
    coala
    Sep 6, 2006
  2. coala
    Replies:
    1
    Views:
    579
    Victor Bazarov
    Sep 6, 2006
  3. Javier
    Replies:
    2
    Views:
    547
    James Kanze
    Sep 4, 2007
  4. RSL
    Replies:
    14
    Views:
    3,221
  5. Disc Magnet
    Replies:
    1
    Views:
    617
    Ian Collins
    May 6, 2010
Loading...

Share This Page