1

According to this response pointers to pointers

char[] = char *p
char[][] = char **p

But, when I initialize an array of strings with the next two forms

char **p = {"Hello", "World"};
char p[][] = {"Hello", "World"};

The compiler shows me some Error.

[Error] array type has incomplete element type

But with

char *matriz[] = {"Hello", "World"};

There is no warning. I'm new in C language, and I'm very confused.

Community
  • 1
  • 1
rvillablanca
  • 1,606
  • 3
  • 21
  • 34
  • C, sorry I will remove the C++ tag – rvillablanca Aug 29 '15 at 14:46
  • 2
    You cannot have dynamic-sized arrays in `C`, and that's not a warning, it is an ERROR – CinCout Aug 29 '15 at 14:47
  • 1
    The answer in the link does not state what you are supposedly quoting!!! – barak manos Aug 29 '15 at 14:51
  • What you quote ,states that `arrays and pointers are same` ? Is that what you meant? – ameyCU Aug 29 '15 at 14:53
  • I understand that if I have `char arr[]`, then `arr = &arr[0]`, sorry but I'm very confused – rvillablanca Aug 29 '15 at 14:57
  • wt is your compiler? Is it gcc? I'm not getting any error with gcc. http://goo.gl/SREx43 – ANjaNA Aug 29 '15 at 15:10
  • With the pointer notation, I get same warnings as you, with `char p[][] = {"Hello", "World"};`, I get the error – rvillablanca Aug 29 '15 at 15:14
  • @GargAnkit there is no dynamic array necessary; dimensions and sizes are statically known at compile time. – Peter - Reinstate Monica Aug 29 '15 at 15:52
  • 3
    Any document that equates `char[][]` with `char **` is not going to be dreadfully helpful; the two are quite different. In fact, `char[]` is not the same as `char *` either, but when you use those notations in the parameter list for a function, they are equivalent (that is: `void something(char data[])` and `void something(char *data)` _are_ equivalent in this one important context). However, `void otherthing(char data[][10])` and `void otherthing(char **data)` are not equivalent at all. – Jonathan Leffler Aug 29 '15 at 16:11
  • Having looked at the other SO question, I don't see where an answer makes the equation you infer from it — which is a relief since it is an SO question. – Jonathan Leffler Aug 29 '15 at 16:18
  • The array of arrays `char ary[3][2]` decays to a pointer to the first element of the array, which will have the type `char (*)[2]` (pointer to array of char). No further decay can occur. This means `char **p;` and `char (*ary_ptr)[2];` are incompatible declarations, and `char **p;` and `char array[3][2];` are also incompatible declarations by extension of the concept. Whoever equated `char[][]` and `char**` is incorrect. –  Aug 29 '15 at 16:33

1 Answers1

6

No an array of array is not the same as a double pointer. You really should read that up, there are a lot of FAQ and answers around.

Then the error that you receive is for the doubly empty [][]. In C, when you declare an array with [] the compiler tries to compute the size of the array from the initializer. This only works for one []. For the second you have to give a length. Something like

char p[][7] = {"Hello", "World"};

should work. For your first form

char **p

this declares a pointer to pointer, so in particular a pointer. Pointers can't be initialized with two items, they need just one, and that should itself be of the correct pointer type or convert to it.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177