-4
int main(int argc, char** argv) {
char a[2][5]={"hell","worl"};

char **p;
p=a;  // error here
cout<<*(*(a+1)+1);
cout<<endl;
cout<<(*a)[2];
return 0;
}

error:

C:\Dev-Cpp\main.cpp [Error] initializer-string for array of chars is too long [-fpermissive]
KillianDS
  • 16,936
  • 4
  • 61
  • 70
yathartha
  • 472
  • 5
  • 8
  • 2
    Arrays are not pointers. You can't assign a 2D array to a double pointer. – chris Sep 26 '13 at 12:44
  • TIL that I forgot that you can't do that, and now I feel bad for being confused as to why this wasn't working. haha – jonhopkins Sep 26 '13 at 12:44
  • but same kind of code works with 1-D array. int main(int argc, char** argv) { int a[2]={1,2}; int *p; p=a; cout<<*p; return 0; } – yathartha Sep 26 '13 at 12:49
  • The error message doesn't match the code. Please double check your question. – avakar Sep 26 '13 at 12:50
  • @user2819483 An `int *p` is a pointer to an int. A `char **p` is a pointer to a pointer to a character. It is not a pointer to a 2D character array. – PVitt Sep 26 '13 at 12:52
  • @chris Don't lie arrays are pointers. And [this](http://stackoverflow.com/questions/17953681/why-pointer-to-pointer-is-a-matrix/17953693#17953693) answer explain why pointer is array and double pointer is matrix. But the part about assignment is correct. So don't lie :) – ST3 Sep 26 '13 at 12:59
  • 2
    @ST3 Arrays are _not_ pointers, and never have been in C or C++. Anything which says the contrary is wrong. (And the answer you cite glosses over quite a few details. In particular, the fact that the `[]` isn't defined on an array type, so that implicit conversions enter into play.) – James Kanze Sep 26 '13 at 13:04
  • 1
    @ST3, [Please provide an example where they *are* the same.](http://coliru.stacked-crooked.com/a/3c90032019ea00ab) – chris Sep 26 '13 at 13:12

2 Answers2

3

Why would you expect it to work? You declare p as char**, and you try to assign a char[2][5] to it. The char[2][5] will convert implicitly to a char (*)[5], but afterwards, you have a pointer, and no further implicit conversions. (EDIT: except to void*.)

If you think about it, it should be obvious. If you dereference a char**, you get a char*. And this char* must reside somewhere in memory, since you have a pointer to it. So where is it?

If you want to iterate over the outer array in your example:

char (*p)[5] = a;
std::cout << *p[0] << sdt::endl;
std::cout << *p[1] << sdt::endl;

Note that your expression *(*(a+1)+1) also supposes that you have an array of pointers somewhere.

Or you can use the usual solution when working with C style strings:

char const* const a[] = { "hell", "worl" };

and

char const* const* p = a;

In this case, you do have an array of pointers, which does implicitly convert to a pointer to a pointer (the first element of the array).

(Of course, the only time you'll really want to use C style strings is with const variables with static lifetimes. In most other cases, std::string is preferable.)

James Kanze
  • 150,581
  • 18
  • 184
  • 329
-1

Other way to access the a[2][5] is,

  char **p=(char**)a;

to get a[0]

 printf("\n a[0] is [%s]", ((char*)p));

to get a[1]

 printf("\n a[1] is [%s]", (((char*)p) + strlen(a[0])+1));

hope this helps.

Saravanan
  • 1,270
  • 1
  • 8
  • 15