I'm really not sure what it is (if it's an array of pointers, or an array of pointer arrays???), but when I step on it via the Debugger it gives me 0xCDCDCDCD, meaning the memory is allocated, but uninitialized. Can anyone show me how to initialize it?
Thanks.
char* (*vars)[4];
I've tried stuff like this, but it gives compile errors:
for (int i = 0; i < 4; i++)
vars[i] = new char*[new char*][4]; // error C2440: 'initializing' : cannot convert from 'char **' to 'unsigned int'
-
6If you're not sure what it is, why are you trying to use it?Oliver Charlesworth– Oliver Charlesworth2012年03月15日 21:17:58 +00:00Commented Mar 15, 2012 at 21:17
-
3I think it would do more good to tell us what you're trying to accomplish, and let us help you do that. I'm hard put to figure out what you'd want to do, that this would be the right way to do it.Jerry Coffin– Jerry Coffin2012年03月15日 21:18:31 +00:00Commented Mar 15, 2012 at 21:18
-
2That looks extremely overcomplicated.Qaz– Qaz2012年03月15日 21:19:02 +00:00Commented Mar 15, 2012 at 21:19
-
To be honest, I have no idea what it is. A former co-worker had it as double, and I am asked to change it to a string (or char*). So I changed it from "double (vars)[4]" to "char (*vars)[4]."SoftwareGuy– SoftwareGuy2012年03月15日 21:27:34 +00:00Commented Mar 15, 2012 at 21:27
-
I ended up using a 2-Dimentional CString array: [CString (*vars)[4];] [vars = new (CString[100][4]);]. This was much easier to instantiate and free. Thank you all for your suggestions.SoftwareGuy– SoftwareGuy2012年03月16日 17:04:58 +00:00Commented Mar 16, 2012 at 17:04
3 Answers 3
Firstly, you need to use cdecl:
declare vars as pointer to array 4 of pointer to char
How you initialise it depends on what your aim is. But, for instance:
// Create a new array
vars = new (char *[1][4]);
// Each element of the array in turn points to an array
for (int i = 0; i < 4; i++) {
(*vars)[i] = new char[27];
}
...
// Cleanup
for (int i = 0; i < 4; i++) {
delete [] (*vars)[i];
}
delete [] vars;
Lastly, you should never write code like this in C++. There's always a better way to do whatever it is you're trying to do (usually involving containers and smart pointers).
4 Comments
vars is a pointer to an array of 4 pointers to char.
You can not nest index new in C++, so you have do so something like:
vars = new (char*)[4];
for(int i=0; i<4; i++) {
(*vars)[i] = ...;
}
The inner part of the loop, the initialization of the pointers within that array, it strongly depends on the rest of your program.
But seriously, if you start writing something like this, whatever medication you're on, take either more, or less.
Comments
double (vars)[4]; just declares an array of 4 doubles. (The optional brackets are just confusing). If you want an array of 4 strings, use char *((vars)[4]); (OK, I went overboard with the brackets.) You can then initialise them to null, or allocate new strings, or point them at preallocated memory.
char *vars[4];
for (int i = 0; i < 4; i++)
vars[i] = new char[I_HOPE_THIS_IS_BIG_ENOUGH];