Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

conversion of 2D array to pointer-to-pointer

Tags:

Activity solution[a][b];  ...  Activity **mother = solution; 

I want to convert 2D array of objects to pointer-to-pointer. How can I do this;

I searched it on google. however I found only one dimension array example.

like image 960
Bahri Gökcan Avatar asked Nov 20 '11 18:11

Bahri Gökcan


People also ask

Is a 2D array a pointer to a pointer?

An array is treated as a pointer that points to the first element of the array. 2D array is NOT equivalent to a double pointer! 2D array is "equivalent" to a "pointer to row".

How do I assign a 2D array to a pointer?

Get the element => *( (int *)aiData + offset ); calculate offset => offset = (1 * coloumb_number)+ 2); Add offset in array base address => (int *)aiData + offset; //here typecast with int pointer because aiData is an array of integer Get the element => *( (int *)aiData + offset );

How do you point a 2D array?

The elements of 2-D array can be accessed with the help of pointer notation also. Suppose arr is a 2-D array, we can access any element arr[i][j] of the array using the pointer expression *(*(arr + i) + j).

Is pointer pointer an array?

Pointer to an array: Pointer to an array is also known as array pointer. We are using the pointer to access the components of the array. int a[3] = {3, 4, 5 }; int *ptr = a; We have a pointer ptr that focuses to the 0th component of the array.


1 Answers

A mere conversion won't help you here. There's no compatibility of any kind between 2D array type and pointer-to-pointer type. Such conversion would make no sense.

If you really really need to do that, you have to introduce an extra intermediate "row index" array, which will bridge the gap between 2D array semantics and pointer-to-pointer semantics

Activity solution[a][b];  Activity *solution_rows[a] = { solution[0], solution[1] /* and so on */ };  Activity **mother = solution_rows; 

Now accessing mother[i][j] will give you access to solution[i][j].

like image 177
AnT Avatar answered Sep 22 '22 23:09

AnT