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 

Trouble understanding pointer to a const array.

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





PostPosted: Sun Mar 07, 2004 9:51 pm    Post subject: Trouble understanding pointer to a const array. Reply with quote



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
Back to top
Victor Bazarov
Guest





PostPosted: Sun Mar 07, 2004 10:35 pm    Post subject: Re: Trouble understanding pointer to a const array. Reply with quote



"Rob" <robpimentel (AT) yahoo (DOT) com> wrote...
Quote:
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?

Quote:
{ "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**.

Quote:
{;}


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



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.