17

I'm very new to C++. I'm trying to call a function that takes in char**:

bool func(char** a) {
    //blablabla
}

So it takes in an array of c-strings. I need to create a char**, but nothing works.

char** a = char[255][255]; // error: type name is not allowed

char** a = new char[255][255]; // error: a value of type "char (*)[255]" cannot be used to initialize an entity of type "char **"

char a[][] = {"banana", "apple"};
char** b = &a; // error: a value of type "<error-type> (*)[2]" cannot be used to initialize an entity of type "char **"

At the end I need to do:

char* a[] = {"banana", "apple"};

Why the first few didn't work and why the last one worked?

Thanks in advance.

1
  • The first few didn't work for exactly the reasons the compiler gave you in the errors. Not sure what kind of other answer you expect here. Code like char **a = char[255][255] is simply not valid C++, so it won't compile.
    – unwind
    Sep 18, 2013 at 10:08

5 Answers 5

12

There's a lot wrong in your code.

char** a = char[255][255]; // error: type name is not allowed

First of all this is not even valid C++ (or C for that matter). Maybe you meant:

char a[255][255];

In any case always remember that the type of a bi-dimensional dynamically allocated array is not ** but (*)[N] which is very different.

char** a = new char[255][255]; // error: a value of type "char (*)[255]" cannot be used to initialize an entity of type "char **"

The error message you provide in the comment explains exactly what I said earlier.

char a[][] = {"banana", "apple"};

In the above code the correct type of the variable a should be char* a[]. Again, arrays and pointer (for what the type is concerned) are very different things. A char array may decay to pointer (if NULL terminated), but for the rest, except with explicit casts, you can't use pointers and arrays like you are doing.

The last one worked because, like I said earlier, char* [] is the correct type for an array of C-strings.

Anyway, if you just doing homework, it is ok to learn this things. But in future development using C++: try not to use "features" that start with C-, like C-strings, C-arrays, etc. C++'s standard library gives you std::string, std::array, std::vector and such for free.

If you really need to allocate dynamic memory (with new and delete, or new[] and delete[]) please use smart pointers, like std::shared_ptr or std::unique_ptr.

0
7

You say you are working in C++. Then you can easily ignore const char* and char** and focus about what you can use:

#include <string>
#include <vector>

std::vector<std::string> arrayOfStrings;
arrayOfStrings.push_back("foo");

bool func(const std::vector<std::string>>& a) {
  ..
}

If you know the size at compile time you can even use std::array:

std::array<255, std::string> fixedArrayOfStrings

EDIT: since you need to build an array of C strings in any case you can easily do it starting from the vector:

const char **arrayOfCstrings = new const char*[vector.size()];

for (int i = 0; i < vector.size(); ++i)
  arrayOfCstrings[i] = vector[i].c_str();

func(arrayOfCstrings);

delete [] arrayOfCstrings;
5
  • 4
    But the function is fixed and not written by me. It's declared as "bool func(char** a)". I can't change that.
    – Boyang
    Sep 18, 2013 at 10:14
  • This won't compile: 1. std::vector<std::string>> (ends with an extra '>') 2. bool func(const <std::vector<std::string>>& a) the extra '<>' around std::vector is also not correct.
    – Teh Suu
    Sep 18, 2013 at 10:27
  • @TehSuu: just a typo because I was writing std::vector<std::vector<std::string>>. Don't ask me why, I still have to completely wake up ;)
    – Jack
    Sep 18, 2013 at 10:31
  • @Jack hehe, happens to us all :))
    – Teh Suu
    Sep 18, 2013 at 10:47
  • I'm curious as to why you went back to using raw new after just introducting std::vector. Why not std::vector<char const*>?
    – Simple
    Sep 18, 2013 at 11:02
2
char**

is ambiguous - it can mean:

  1. pointer to pointer
  2. array of c-strings - experienced programmer would write char* arr[] instead

In the first case it is quite simple:

char* niceString = GetNiceString();
func(&niceString);

however in the second case it is slightly more complex. The function will not know the length of the array so you need to end it explicitly with a NULL, just like for example environ is:

char* a[3] = { "One", "Two", NULL }; /* note that this is possibly  dangerous
because you assign const char* (READ-ONLY) to char* (WRITABLE) */
func(a); // char*[] gets downgraded to char** implicitly
0

so char** a = char[255][255]; it's weird to C and C++ and if you want a static 2d array just

char a[255][255];
1
  • 1
    char[255][255] is not convertible to char** (since it's contiguous memory of 255 * 255 chars). It should be char *a[255]; (255 pointers to c-strings somewhere, which can be converted to pointer to first c-string, i.e. char**).
    – JJJ
    Sep 18, 2013 at 10:57
0

char ** is a scalar type, you have to cast to (char *[]), try this :

char **temp = (char *[]){"abc", "def","fg"};
1
  • 1
    A nice trick for C, but wouldn't work in C++. Apr 30, 2022 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.