Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't we use double pointer to represent two dimensional arrays?

Tags:

c

Why can't we use double pointer to represent two dimensional arrays?

arr[2][5] = {"hello","hai"}; **ptr = arr; 

Here why doesn't the double pointer (**ptr) work in this example?

like image 664
Thangaraj Avatar asked Dec 17 '10 13:12

Thangaraj


People also ask

Is a double pointer a 2D array?

No. A multidimensional array is a single block of memory.

How can use 2D array in double pointer?

"#include int main() { int arr[][4]={ {12,23,34,45}, {56,67,78,89} }; int **ptr=NULL; ptr=(int **)arr; printf("n The array first element Access %d %d",arr[0][0],*ptr); ptr++; printf("n The array first element Access %d %d",arr[0][0],*ptr); printf("n The array first element Access %d %d",arr[0][0],*(*(ptr+0)+0)); printf ...

What is a double pointer array?

A pointer is used to store the address of variables. So, when we define a pointer to pointer, the first pointer is used to store the address of the second pointer. Thus it is known as double pointers.

How do you assign a two-dimensional array to a pointer?

The simple answer is that you cannot. A bidimensional array is a contiguous block of memory that holds each line, while a pointer to pointer can refer to a memory location where a pointer to a different memory location containing the integers is.


1 Answers

I'm going to try to draw how

int array[10][6]; 

and

int **array2 = malloc(10 * sizeof *array2); for (int i = 0; i < 10; ++i)     array2[i] = malloc(6 * sizeof **array2); 

look like in the memory and how they are different (And that they can't be cast to each other)

array looks like:

 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ | | | | | | | | | | | | | ..............| | | (10*6 elements of type int)  - - - - - - - - - - - - - - - - - - - - - - < first row >< second row> ... 

array2 looks like:

 _ _ _ _ _ _ _ _ _ _  | | | | | | | | | | | (10 elements of type int *)  - - - - - - - - - -   | |     ....      |     _ _ _ _ _ _  | |                \-->| | | | | | | (6 elements of type int)  | |                     - - - - - -  | |  | |      _ _ _ _ _ _  |  \ -->| | | | | | | (6 elements of type int)  |        - - - - - -  |  |  |      _ _ _ _ _ _   \ -->| | | | | | | (6 elements of type int)         - - - - - - 

When you say array[x][y], it translates into *((int *)array+x*6+y)

While, when you say array2[x][y], it translates into *(*(array2+x)+y) (Note that for array, this formula also works (read to the end of the post, and then the comments)).

That is, a static 2d array is in fact a 1d array with rows put in one line. The index is calculated by the formula row * number_of_columns_in_one_row + column.

A dynamic 2d array, however is just a 1d array of pointers. Each pointer then is dynamically allocated to point to another 1d array. In truth, that pointer could be anything. Could be NULL, or pointing to a single variable, or pointing to another array. And each of those pointers are set individually, so they can have different natures.

If you need to pass the pointer of array somewhere, you can't cast it to int ** (imagine what would happen. The int values of the cells of array are interpreted as pointers and dereferenced -> Bam! Segmentation fault!). You can however think of array as a 1d array of int [6]s; that is a 1d array of elements, with type int [6]. To write that down, you say

int (*p)[6] = array; 
like image 180
Shahbaz Avatar answered Sep 30 '22 12:09

Shahbaz