1

I have something being returned as a const char* and would like to save it to an array.

I've tried this:

const char* book[amtBooks] = "";

and get this error:

error: array must be initialized with a brace-enclosed initializer
1
  • How about with const char* book[amtBooks] = { NULL };. This will initialize every book to a null pointer.
    – Kwasmich
    Aug 20, 2018 at 16:01

1 Answer 1

2
  • const char* book[amtBooks] is an array of pointers.
  • "" is an array of chars (with only a NUL character).

You can initialize an array of chars with an array of chars:

const char foo[] = "hello";

You can also initialize a pointer to char with an array of chars:

const char *bar = "good bye";

this works because of the “decay to pointer” feature of C and C++.

But initializing an array of pointers with an array of chars simply does not make sense. An array of pointers to char could be initialized as

const char *book[] = {"hello", "good bye"};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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