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 

C++ static character 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
John Ratliff
Guest





PostPosted: Tue Jan 27, 2004 8:10 pm    Post subject: C++ static character array... Reply with quote



How can you define a static character pointer array in C++?

ex:

class tmp {
private:
static const char *ARR[];
};

const char *tmp::ARR[] =
{
"nts1", "nts2", "nts3"
}

--- This doesn't compile for me, even if I switch it to use two *'s and
abandon the [].

---

However, this is valid:

static const char *ARR[] =
{
"nts1", "nts2", "nts3"
}

This will work, but I would like to know how to embed the declaration
inside a class.

Thanks,

--Dominic

Back to top
Rob Williscroft
Guest





PostPosted: Tue Jan 27, 2004 8:27 pm    Post subject: Re: C++ static character array... Reply with quote



John Ratliff wrote in news:bv6gis$ijm$1 (AT) hood (DOT) uits.indiana.edu:

Quote:
How can you define a static character pointer array in C++?

ex:

class tmp {
private:
static const char *ARR[];
};

const char *tmp::ARR[] =
{
"nts1", "nts2", "nts3"
}

;

Quote:


With the missing ; added this compiles fine, what is your error message.

Quote:
--- This doesn't compile for me, even if I switch it to use two *'s and
abandon the [].


Rob.
--
http://www.victim-prime.dsl.pipex.com/

Back to top
John Ratliff
Guest





PostPosted: Thu Jan 29, 2004 4:35 pm    Post subject: Re: C++ static character array... Reply with quote



-- temp.cc --
#include <iostream>

using namespace std;

class tc {
private:
static const char **ARR;
public:
void print();
};

const char **tc::ARR =
{
"hello", "world", "there"
};

void tc::print() {
for (int i = 0; i < 3; i++) {
cout << ARR[i] << endl;
}
}

int main(int argc, char **argv) {
tc app;

app.print();

return 0;
}
--- end of temp.cc

g++ temp.cc
test.cc:15: initializer for scalar variable requires one element

What is the difference between

const char **var and const char *var[]?

--Dominic

"Rob Williscroft"
Quote:
John Ratliff wrote in news:bv6gis$ijm$1 (AT) hood (DOT) uits.indiana.edu:

How can you define a static character pointer array in C++?

ex:

class tmp {
private:
static const char *ARR[];
};

const char *tmp::ARR[] =
{
"nts1", "nts2", "nts3"
}

;



With the missing ; added this compiles fine, what is your error message.

--- This doesn't compile for me, even if I switch it to use two *'s and
abandon the [].


Rob.
--
http://www.victim-prime.dsl.pipex.com/



Back to top
Ron Natalie
Guest





PostPosted: Thu Jan 29, 2004 4:51 pm    Post subject: Re: C++ static character array... Reply with quote


"John Ratliff" <jdr (AT) nospam (DOT) com> wrote


Quote:

What is the difference between

const char **var and const char *var[]?

The first is a pointer to a pointer to const char.

The second is an array (length unspecified) of pointers to const char.

Despite much confusion on newbie programmers part and the fact that
there is an implicit array to pointer conversion, ARRAYS and POINTERS
ARE NOT THE SAME THING.

const char**x = { "a", "b" };

fails because you are trying to use an aggregate initalizer to initialize something
that's not an aggregate. x points to exactly one thing, which is another pointer.

const char * x[] = { "a", "b" };

works because x is now an array of multiple things, so you can use the aggregate
initializer to set each of them. The things inside the {} are now valid initializers
for each const char* that is in the array.

Back to top
John Ratliff
Guest





PostPosted: Thu Jan 29, 2004 5:04 pm    Post subject: Re: C++ static character array... Reply with quote


"Ron Natalie" <ron (AT) sensor (DOT) com> wrote

Quote:

"John Ratliff" <jdr (AT) nospam (DOT) com> wrote



What is the difference between

const char **var and const char *var[]?

The first is a pointer to a pointer to const char.
The second is an array (length unspecified) of pointers to const char.

Despite much confusion on newbie programmers part and the fact that
there is an implicit array to pointer conversion, ARRAYS and POINTERS
ARE NOT THE SAME THING.

const char**x = { "a", "b" };

fails because you are trying to use an aggregate initalizer to initialize
something
that's not an aggregate. x points to exactly one thing, which is
another pointer.

const char * x[] = { "a", "b" };

works because x is now an array of multiple things, so you can use the
aggregate
initializer to set each of them. The things inside the {} are now valid
initializers
for each const char* that is in the array.

Thanks.

P.S. Sorry for top-posting. I didn't know...

---
"I'm learnding." (--Ralph Wiggum)



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.