473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

C++ static character array...

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

Jul 22 '05 #1
4 15166
John Ratliff wrote in news:bv**********@hood.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/
Jul 22 '05 #2
-- 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" <rt*@freenet.REMOVE.co.uk> wrote in message
news:Xn**********************************@195.129. 110.131...
John Ratliff wrote in news:bv**********@hood.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/

Jul 22 '05 #3

"John Ratliff" <jd*@nospam.com> wrote in message news:10*************@corp.supernews.com...

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.
Jul 22 '05 #4

"Ron Natalie" <ro*@sensor.com> wrote in message
news:40*********************@news.newshosting.com. ..

"John Ratliff" <jd*@nospam.com> wrote in message news:10*************@corp.supernews.com...

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)
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
5
by: IamZadok | last post by:
Hi I was wondering if anyone knew how to convert a string or an integer into a Static Char. Thx
5
by: Crimzon | last post by:
I am using MSVC++ 6.0 compiler. I am declaring an array char ch. Program works fine for these arbitrary sizes. But if I make the size of the array bigger like ch, the program gives me an error...
15
by: ehabaziz2001 | last post by:
Hi, Till now I do not understand how the null character automatically added to the end of the string and it is not an element of the string array . All books said the null character (\0) added...
11
by: dis_is_eagle | last post by:
hi...i am new to c programming....please explain me as to why should an character array be declared as static...thanx...eric
7
by: arnuld | last post by:
i am trying to implement C style strings in C++ (from chapter 4 "C++ Primer 4/e"): // reading from std::cin for a c-string // (a null terminated character array) #include <iostream> #include...
14
by: Shhnwz.a | last post by:
Hi, I am in confusion regarding jargons. When it is technically correct to say.. String or Character Array.in c. just give me your perspectives in this issue. Thanx in Advance.
6
by: mathieu | last post by:
Hi there I am trying to provide a lookup from two 'int's into a char array, something like this: template <int g, int estruct Lookup; template <struct Lookup<0,0{ // some typedef + enums...
2
by: chenxinleo | last post by:
Hi, When i use some standard library functions and fields,which return char* type(like ctime in time.h, optarg in getopt.h)and do not have to be freed after calling,i always worry about memory...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.