2

I have a method that I'm trying to call. The prototype looks something like this:

void MyMethod(const char* names[])

I want to construct an array of char* to pass to this method, but I cannot workout the syntax. About the best I can do is:

char* names[] = new char*[numNames];

But this does not compile. Any ideas?

Note: I cannot change the declaration of MyMethod, that's set in stone.

MrMoDoJoJr
  • 390
  • 3
  • 9

3 Answers3

2

While everyone else here seems hung up on (correctly) addressing your syntax (the trailing []) I suspect your issue you've struggled with at present is how to pass a pointer-to-pointer-to-const-char. The original syntax you provided is wrong, but doing this:

char **names = new char *[numNames];

// populate names[]

MyMethod(names);

won't exactly solve your problem. You'll receive a "no matching function call" error because the parameter you're passing is of a specific type (pointer-to-pointer-to-char), while the function is expecting a non-trivially-convertable different type (pointer-to-pointer-to-const-char).

The simple fact is, you can't do it from char **. See this question/answers for the reasons why, but the long and short of it is the types are simply not compatible. If you want to do this without changing that prototype you're going to have to do something like this:

int numNames = 5;
char **names = new char *[numNames];

// populate your names[], then...

const char **ro_names = new const char *[numNames];
std::copy(names, names+numNames, ro_names);

MyMethod(ro_names);

delete [] ro_names;
// clean up names[] elements, then...
delete [] names;

Hideous I know. You can clean it up considerably using a vector (which you should be using anyway, btw), but I kept the shuddering dynamics as they were what you started with.

Community
  • 1
  • 1
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • Thanks, this prodded me in the right direction. I had a char** in an earlier iteration of the code and believed the const should just get inferred, but seemingly not. PS it seems I can get away without the std::copy if I add the const to my initial new. – MrMoDoJoJr Jan 28 '14 at 17:16
  • @MrMoDoJoJr True, it just makes it more tedious to initialize the new one (*your* new one; not the code above). I would have mention it but was fairly convinced you needed writable content in your array, and knew the above would work regardless. Anyway, Glad it helped. – WhozCraig Jan 28 '14 at 17:21
0

I have the following idea:

char** names = new char*[numNames];

By the way the following function declarations declare the same function

void MyMethod(const char* names[]);
void MyMethod(const char** names);
void MyMethod(const char* names[10]);
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0
char** names = new char*[numNames];

The syntax in brief:

if array was just the array of char we would say:

char* arr = new char[10]
 ^  ^             ^
 T  P             T

T - type

P - pointer (because new returns a pointer (if successful, or throws bad_alloc))

thus, the array of char* is:

char* * arr = new char*[10]
 ^    ^             ^
 T    P             T
4pie0
  • 29,204
  • 9
  • 82
  • 118