how to initialize const char array?
-
Hi, The following is the example I have (and works):
const char *labels[] = {"Mon", "Tue", "Wed"}
SomeFunctions(labels, 3);Now I want to initialize the contents of the array at run-time. I tried the following but an error comes up:
const char *labels= NULL;
*labels = new char[3];
labels[0]=_T("Mon");
labels[1]=_T("Tue");
labels[2]=_T("Wed");
SomeFunctions(labels, 3);error C2665: SomeFunctions : none of the 3 overloads can convert parameter 1 from type 'const char *'
Can someone please show me how declare the same type of array as the examples? Thanks.
-
Hi, The following is the example I have (and works):
const char *labels[] = {"Mon", "Tue", "Wed"}
SomeFunctions(labels, 3);Now I want to initialize the contents of the array at run-time. I tried the following but an error comes up:
const char *labels= NULL;
*labels = new char[3];
labels[0]=_T("Mon");
labels[1]=_T("Tue");
labels[2]=_T("Wed");
SomeFunctions(labels, 3);error C2665: SomeFunctions : none of the 3 overloads can convert parameter 1 from type 'const char *'
Can someone please show me how declare the same type of array as the examples? Thanks.
Joe Smith IX wrote:
*labels = new char[3];
This is wrong. labels = new char *[3]; looks better. You want to create three char *s, not an array of three chars
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
Hi, The following is the example I have (and works):
const char *labels[] = {"Mon", "Tue", "Wed"}
SomeFunctions(labels, 3);Now I want to initialize the contents of the array at run-time. I tried the following but an error comes up:
const char *labels= NULL;
*labels = new char[3];
labels[0]=_T("Mon");
labels[1]=_T("Tue");
labels[2]=_T("Wed");
SomeFunctions(labels, 3);error C2665: SomeFunctions : none of the 3 overloads can convert parameter 1 from type 'const char *'
Can someone please show me how declare the same type of array as the examples? Thanks.
can you please show us how is declared SomeFunction(), and what you're trying to do exactly... just a guess: why aren't you using
std::vector<>
? -- modified at 4:39 Wednesday 2nd May, 2007 [edit] what about overloading SomeFunction() intoSomefunction(const std::vector<std::string>&)
and use this code instead :std::vectorstd::string vec;
vec.push_back("mon");
vec.push_back("tue");
vec.push_back("wed");SomeFunction(vec);
[/edit]
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
Joe Smith IX wrote:
*labels = new char[3];
This is wrong. labels = new char *[3]; looks better. You want to create three char *s, not an array of three chars
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
oops, you are right. But now that I changed it as you suggested, it added a new error:
error C2440: '=' : cannot convert from 'char ** ' to 'const char *'
-
can you please show us how is declared SomeFunction(), and what you're trying to do exactly... just a guess: why aren't you using
std::vector<>
? -- modified at 4:39 Wednesday 2nd May, 2007 [edit] what about overloading SomeFunction() intoSomefunction(const std::vector<std::string>&)
and use this code instead :std::vectorstd::string vec;
vec.push_back("mon");
vec.push_back("tue");
vec.push_back("wed");SomeFunction(vec);
[/edit]
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
This function is from a lib. here is the declaration in the header file (Somefunction=StringArray)
class StringArray { public : int len; const char * const *data; StringArray() : len(0), data(0) {} StringArray(const char * const *data, int len) : len(len), data(data) {} const char *operator[](int i) const { return data[i]; } };
-
This function is from a lib. here is the declaration in the header file (Somefunction=StringArray)
class StringArray { public : int len; const char * const *data; StringArray() : len(0), data(0) {} StringArray(const char * const *data, int len) : len(len), data(data) {} const char *operator[](int i) const { return data[i]; } };
Joe Smith IX wrote:
StringArray(**
const char * const *
**data, int len)oh my god !!! does this really compile, and even if so, is this really necessary, rather that
const char*
?? and is this class yours ? why using C style stuff in a C++ code ? (i mean,char*
strings when you obviously use classes, so, could usestd::string
...)
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
Joe Smith IX wrote:
StringArray(**
const char * const *
**data, int len)oh my god !!! does this really compile, and even if so, is this really necessary, rather that
const char*
?? and is this class yours ? why using C style stuff in a C++ code ? (i mean,char*
strings when you obviously use classes, so, could usestd::string
...)
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
like i wrote before, it's not my class. it's part of a lib i am using. and i am bit confused myself (since I always use the easier path - MFC :) so, is there anyway to get around this?
-
oops, you are right. But now that I changed it as you suggested, it added a new error:
error C2440: '=' : cannot convert from 'char ** ' to 'const char *'
const char *labels= NULL;*labels = new char[3];labels[0]=_T("Mon");labels[1]=_T("Tue");labels[2]=_T("Wed");SomeFunctions(labels, 3); Something like char ** labels = new char *[3]; labels[0] = "test"; label[1] = "Not sure if this is exactly right, you may need to use new and strcpy"; label[2] = "I agree with the other poster, you should use vector if you can"; In fact, a vector is a dynamic array, so if you use char * instead of string, the index of the first char in the first item, would represent the start of a block of memory with three strings in it.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
const char *labels= NULL;*labels = new char[3];labels[0]=_T("Mon");labels[1]=_T("Tue");labels[2]=_T("Wed");SomeFunctions(labels, 3); Something like char ** labels = new char *[3]; labels[0] = "test"; label[1] = "Not sure if this is exactly right, you may need to use new and strcpy"; label[2] = "I agree with the other poster, you should use vector if you can"; In fact, a vector is a dynamic array, so if you use char * instead of string, the index of the first char in the first item, would represent the start of a block of memory with three strings in it.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
Yup, you nailed it exactly. Thanks a lot. P.S.: Since this is not my class, I cannot change it.