473,791 Members | 3,090 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 15214
John Ratliff wrote in news:bv******** **@hood.uits.in diana.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.RE MOVE.co.uk> wrote in message
news:Xn******** *************** ***********@195 .129.110.131...
John Ratliff wrote in news:bv******** **@hood.uits.in diana.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.supe rnews.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******** *************@n ews.newshosting .com...

"John Ratliff" <jd*@nospam.com > wrote in message news:10******** *****@corp.supe rnews.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
4594
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: VarArray::funct were an extern, but it is declared in the same file (q.v.). What is the remedy for this? =================
5
6196
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
2013
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 message pertaining to the array size and doesnt work. Does it mean that the total size of the array allocated (10000 * 20) should be less than 65535 or something like that ? The compiler help file tells that the size of data type character is 1...
15
2152
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 automatically to the end of the string. Let say char name="123456789" If entered the name in a loop;
11
3227
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
7785
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 <string> int main() {
14
4094
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
5167
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 definitions static const char A; };
2
11713
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 leaking(thoug i konw i just donot have to).Then i look inside in time.h file(mingw) ,and i found notes say"These functions write to and return pointers to static buffers that may be overwritten by other function calls".So how is the"static buffers"...
0
9669
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10427
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10155
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9995
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5431
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4110
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.