Trouble understanding pointer to a const array.

R

Rob

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
 
V

Victor Bazarov

Rob said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top