C++ static character array...

J

John Ratliff

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
 
R

Rob Williscroft

John Ratliff wrote in
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.
 
J

John Ratliff

-- 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 << 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 said:
John Ratliff wrote in
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.
 
R

Ron Natalie

John Ratliff said:
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.
 
J

John Ratliff

Ron Natalie said:
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...
 

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,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top