5

Recently, I need to declare a string array, so I wrote down the following statement:

const char** directories = {"cricket_batting", "cricket_bowling", "croquet", "tennis_forehand", "tennis_serve", "volleyball_smash"};

However, g++ showed me the error:

error: scalar object ‘directories’ requires one element in initializer

So I changed the statement to this:

const char* directories[] = {"cricket_batting", "cricket_bowling", "croquet", "tennis_forehand", "tennis_serve", "volleyball_smash"};

This time, it was right. But I can't exactly know the difference between char** and char[].

leemes
  • 44,967
  • 21
  • 135
  • 183
alfredtofu
  • 117
  • 1
  • 6
  • 2
    The first one is a pointer to a pointer, the second one is an array of pointers. `{ ... }` is used for arrays (and structures) but it is not used for pointers. That's why your first one didn't compile. – john Apr 03 '13 at 14:14
  • @JoachimPileborg I didn't feel there was enough depth. It's a big subject, and I didn't think the OP would read my three sentences and suddenly understand. – john Apr 03 '13 at 14:16
  • @john I know what you said. Maybe I can't express my question clearly.. The answer accepted is perfect. Thx! – alfredtofu Apr 04 '13 at 08:41

3 Answers3

9
= {...};

Initialisation of this form is known as list-initialization.

const char**

This type is a "pointer to pointer to const char".

const char*[]

This type is an "array of pointer to const char".

Simply put, you cannot initialise a pointer with list-initialization. You can initialise an array with list-initialization; it initializes each of the elements in the array with the items in the braced list.

The reason comes down to what exactly you get from a declaration. When you declare a const char**, all you get is a single pointer object. It's a const char**, which is a pointer promising to point at a pointer to const char. Nonetheless, all you actually have is the outer pointer. You can't then initialise this as though it's an array, since you only have one pointer. Where exactly are you going to store the elements of the initialization list? There is no array of pointers in which you can store them.

However, when you declare a const char*[], what you get is an array of pointers. The size of the array is determined by the size of the list because you have omitted it.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
  • This post also explains it: http://stackoverflow.com/questions/7564033/difference-between-char-and-char?rq=1 – David Apr 03 '13 at 14:15
1

The former is a pointer to a pointer to const char while the latter is an array to pointer to const char. The latter is the correct way to initialize an array of strings.
You would need to allocate memory using new to set up char** , for you can't simply initialize pointers with { }.

bash.d
  • 13,029
  • 3
  • 29
  • 42
0

When you write int a[] we are making an array of integers. Similarly when you write const char* directories[] you are creating an array of char* pointers. Now each char* can point to a char or a string as in your case. This creates individual string constants and assigns the base address of each to the corresponding pointer in the array

char **directories is pointer to a pointer and you can't assign value to a pointer using { .. }

Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59