assigning 2D arrays to pointers

This is a discussion on assigning 2D arrays to pointers within the C Programming forums, part of the General Programming Boards category; As we know to assign an array to pointers we just need to write as e.g., Code: int *p,x[10]; p=x; ...

1. assigning 2D arrays to pointers

As we know to assign an array to pointers we just need to write as
e.g.,
Code:
```int  *p,x[10];
p=x;```
we just need to assign the array name to the pointer without any index.But what about 2D arrays.How to assign a 2D array to a pointer.
e.g.;
Code:
`int *p,x[][2]={1,2,3,4,5,6};p=x;`
whether for assigning x[][2] to p it should be like the 1D array only or other.Please reply now,I am working on it now.....Yours help is welcomed

2. First, this:
Code:
`x[][2]={1,2,3,4,5,6}`
is not valid.

Anyway, for a pointer to (eg) x[10][10] you can use:
Code:
`(*ptr)[10][10] = &x;`
and access elements with (*ptr)[2][7], etc.

3. With a regular 2D array, (not a pointer to an array), couldn't you use a regular pointer?

Would you need a pointer to a pointer before the array was degraded to a double pointer?

After the array has degraded, I'd think you'd want a pointer to a pointer for a 2D array.

4. reply yo MK27

Originally Posted by MK27
First, this:
Code:
`x[][2]={1,2,3,4,5,6}`
is not valid.

Anyway, for a pointer to (eg) x[10][10] you can use:
Code:
`(*ptr)[10][10] = &x;`
and access elements with (*ptr)[2][7], etc.
hello MK27, first i want to say when you replied i was offline and so couldn't reply instantly.
Anyway the initialization of 2D arrays that you said is not valid is perfectly valid.I tried in codeblocks and works fine.
i also want to ask why do we need to assign as &x (x being a 2D array) to a pointer bcoz the name of the array itself is an address of the first element.is it always mandatory to express the sizes explicitly while assigning.
So it is found the assignment of a multidimensional array to a pointer is completely different from a1D array,is it right????anyone can comment on it please.....

5. Originally Posted by Adak
With a regular 2D array, (not a pointer to an array), couldn't you use a regular pointer?

Would you need a pointer to a pointer before the array was degraded to a double pointer?

After the array has degraded, I'd think you'd want a pointer to a pointer for a 2D array.
Hello Adak,are you asking me or replying.....
anyway a regular pointer cannot be used for the assignment of 2D array.
I think pointer to a pointer would make the program more complex. I am afraid that i dont know to use it here.

6. Originally Posted by rakeshkool27
So it is found the assignment of a multidimensional array to a pointer is completely different from a1D array,is it right????anyone can comment on it please.....
I believe a better example is:
Code:
```int (*ptr)[10];
int a[10][10];
ptr = a;```
which is like a pointer to 1D array. What you have to keep in mind is that you can "trade" a pointer for one dimension.

This is NOT valid, you cannot trade two pointers for 2D dimensions:
Code:
```int **ptr;
int a[10][10];
ptr = a; //compile error```
But you can do this
Code:
```int **ptr;
...
ptr[0][1] = 0;```
provided that ptr[0][1] points at a valid memory location.

This might seems confusing. But you just have to remember the "trade one dimension for one pointer" thing.

More in depth:
The reason you are not allowed to do the 2nd code is that an multi-dimensional array in C is not like a pointer. Here is the difference of a 2D array and a pointer to a pointer. Consider this:
Code:
```ptr[1][1] = 0; //ptr is int**
a[1][1] = 0; //a is a[10][10]```
for the first, the compiler will do this
1) Read the value of ptr. Add the first index. Thus ptr+1
2) Read where ptr+1 points. Thus read *(ptr+1)
3) Add the second index to that. Thus *(ptr+1)+1
4) Assign the value 0 where that points to. Thus do *(*(ptr+1)+1) = 0

What is the difference in the second case. The compiler knows the size. Thus, it can optimize the process. It will simply do this:
0) Assume that MAX_X = 10 and MAX_Y =10. And you have a[x][y]. Thus here, x=1, y=1
1) Read the value of a. Add MAX_Y*x + y, thus a+10*1+1, thus a+11
2) Assign the value 0 where that points to. Thus do *(a+11) = 0

In other words, [x][y][z] = [MAX_X*MAX_Y*x + MAX_Y*y + z]

Note now why the compiler doesn't need to know all dimensions. Just all but one. Because declaring this
Code:
`x[][2]={1,2,3,4,5,6}`
is valid, because teh compiler will figure out that you have tottally 6 elements. Thus you actually have x[3][2].

What is the difference with
Code:
`int (*ptr)[10]`
??
The difference is that you give the compiler an info. That MAX_X = 10. Notice that MAX_Y is not needed. The formula is [x][y] = [MAX_X * x + y]. So the compiler thas all necessary information to translate indexes. Put if you do
Code:
`int **ptr;`
the compiler wouldn't know about MAX_X. Thus you are not allowed to do so.

So when you have this:
[x][y] it can actually mean two things
1) *(array + MAX_X * x + y) if it is after an array
2) *(*(pointer+x)+y) if it is after a pointer
the bad thing is that in some books the first is not clarified

More in debth:
I mentioned "optimize". The bad thing with a pointer to a pointer is that you have an array of pointers (one piece of memory) and each pointer in the array points to another array. If you have ptr[10][10], you have an array of 10 pointers and each pointer points to an array of 10 integers. Now, if you have a[10][10] you have a big array with 100 integers. A contiguous space, which is always more beneficial. You will have more chances for a cache hit, for example.

Popular pages Recent additions