Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize char**

Tags:

c++

string

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.

like image 558
Boyang Avatar asked Sep 18 '13 10:09

Boyang


People also ask

How do you initialize a char?

To initialize a char in Java, we can use any char value such as an empty char, or \0 , or even a char value itself. While creating a char variable, we must first understand if the declared variable is local or instance because if the variable is local, then we must initialize it at the time of declaration.

How do I initialize a char in C++?

To declare a char variable in C++, we use the char keyword. This should be followed by the name of the variable. The variable can be initialized at the time of the declaration. The value of the variable should be enclosed within single quotes.

How do you initialize a char array?

You can initialize a one-dimensional character array by specifying: A brace-enclosed comma-separated list of constants, each of which can be contained in a character. A string constant (braces surrounding the constant are optional)


2 Answers

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.

like image 176
Shoe Avatar answered Sep 27 '22 19:09

Shoe


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;
like image 31
Jack Avatar answered Sep 27 '22 19:09

Jack