RaKo
RaKo

Reputation: 77

pointer to 2d array of pointers

How would I declare a pointer which points to 2D array of character pointers. Like

char *ar[10][10]; 

In my understanding this array is stored as array of array, so ar points to a array of pointers, each of which points to a column in this array. So it has three level of pointers. So it should be declared as

char ***p; 

Thus both ar and p are of the same type. But if I use p like a 2d array for e.g. p[0][0], it gives a segmentation fault. Why does this happen and what would be the correct way to declare p?

Upvotes: 0

Views: 160

Answers (4)

Vlad from Moscow
Vlad from Moscow

Reputation: 311088

The declaration will look like

char * ar[10][10]; 

char * ( *p_ar )[10][10] = &ar; 

Dereferencing the pointer for example in the sizeof operator you will get the size of the array because the expression will have type char[10][10]

printf( "%zu\n", sizeof( *p_ar ) );

The output will be equal to 100

To output for example the first string of the first row of the original array using the pointer you can write

printf( "%s\n", ( *p_ar )[0][0] );

To output the first character of the first string of the first row of the original array using the pointer you can write

printf( "%c\n", ( *p_ar )[0][0][0] );

or

printf( "%c\n", *( *p_ar )[0][0] );

You could also declare a pointer to the first element of the array

char * ar[10][10]; 

char * ( *p_ar )[10] = ar; 

In this case to output the first string of the first row of the original array using the pointer you can write

printf( "%s\n", p_ar[0][0] );

To output the first character of the first string of the first row of the original array using the pointer you can write

printf( "%c\n", p_ar[0][0][0] );

or

printf( "%c\n", *p_ar[0][0] );

Upvotes: 1

jacob
jacob

Reputation: 681

The declaration is correct,
p[0][0] is just a pointer to char*,may be you access it before alloc storage to it,so you get a segment fault

Upvotes: 0

Lundin
Lundin

Reputation: 214890

Arrays are not pointers and pointers are not arrays. Similarly, an array of arrays has absolutely nothing to do with a pointer to pointer. If someone told you differently they were confused.

char* ar[10][10] is a 2D array of char pointers. It can also be regarded as an array of arrays of char pointers.

If you want a pointer to such an array, you would use an array pointer:

char* (*ptr)[10][10]

This is read as

  • (*ptr) Array pointer to...
  • [10][10] ...a 10x10 array...
  • char* ...of char* items.

Upvotes: 0

glglgl
glglgl

Reputation: 91139

Your misconception is that each of the levels would be a pointer. This is not true.

char ***p means that you have a pointer which points to a pointer which points to a pointer which points to a char. But that's not what you have.

Your char[10][10] is stored as 100 chars in a contigouos way, every 10 of them forming a char[10]. So either you indeed use a char *ar[10][10] or you switch to using a char *ar and use that for addressing the array in a 1D fashion. This may have advantages, but is generally only recommended if the array's shape is about to be variable.

Upvotes: 0

Related Questions