# Pointers and 2DArray

• 02-18-2009
ganesh bala
Pointers and 2DArray
In below code, I am getting following error.. why it happens? .. i

How to assign it to pointer str? [ without changing a[5][10] to other notation]

cannot convert `char (*)[10]' to `char**' in initialization

Code:

``` int main() {         char a[5][10]={"One", "Two", "Three", "Four", "Five" };         char **str=a;                                                            // This is not Ok  ???         char *b[5]={"One", "Two", "Three", "Four", "Five" };         char **str1 = b;  // this is Ok         printf("%d %d %d %d\n", sizeof(a), sizeof(str), sizeof(b), sizeof(str1));         printf("%p %p %p %p %p %p\n", a, str, &a[1], &str[1], &a[1][1], &str[1][1]);         printf("%p %p %p %p %p %p\n", b, str1, &b[1], &str1[1],                                 &b[1][1], &str1[1][1]);                                 getchar();                                 return 0; }```
• 02-18-2009
matsp
Since char [5][10] is not in itself an array of pointers, there is no simple way to make it into a pointer to pointers. You simply can not do that.

--
Mats
• 02-18-2009
Salem
> char **str=a;
But this should be
char (*str)[10] = a;

That is str is a pointer to an array of 10 characters.

Without the (), it's just an array of 10 pointers to char, which is something completely different.
• 02-18-2009
itCbitC
you can put an intermediate step to achieve that as in.
Code:

```char a[5][10]={"One", "Two", "Three", "Four", "Five" }; char *p = &a[0][0]; char **str = &p;```
• 02-18-2009
matsp
Quote:

Originally Posted by itCbitC
you can put an intermediate step to achieve that as in.
Code:

```char a[5][10]={"One", "Two", "Three", "Four", "Five" }; char *p = &a[0][0]; char **str = &p;```

And that makes a single pointer to pointer, so if you later try to use str[1], it will point to some random place in the memory - not at "Two".

And
Code:

`char *p = a[0];`
is a bit simpler to write what you did.

Now, if you really want to be able to access the 5 elements in the original array with a pointer to pointer, you need to have a char *p[5], and set each element to a[0], a[1], a[2], etc.

--
Mats
• 02-18-2009
Snafuist
Quote:

Now, if you really want to be able to access the 5 elements in the original array with a pointer to pointer, you need to have a char *p[5], and set each element to a[0], a[1], a[2], etc.
This is not quite true, although I wouldn't recommend anyone to do that:

Code:

```        char a[5][10]={"One", "Two", "Three", "Four", "Five"};         char *p = a[0];         char **str = &p;         printf("%s\n", (*str + n*10));```
Now you can use the printf() to display the n-th element of a. But this is of course much the same as simply writing *&p instead of *str in the printf(). On the other hand, its seems senseless to use a pointer to a pointer in the first place anyway.

Greets,
Philip
• 02-18-2009
matsp
I guess that is in return for the "you should not leave stuff uninitialized": Yes, of course, using the right arithmetic will make any pointer point to any other place in memory, so if it is possible to calculate the right place, we can do that. But it's hardly an OBVIOUS way to do it. In my defence, my comment was referring to so if you later try to use str[1]... (although I should, in hindsight [doh!], have made the reference more explicit).

--
Mats