0

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?

tversteeg
  • 4,717
  • 10
  • 42
  • 77
RaKo
  • 49
  • 1
  • 2
  • 11
  • 1
    I think you will get some idea from here http://stackoverflow.com/questions/13554244/how-to-use-pointer-expressions-to-access-elements-of-a-two-dimensional-array-in – Janmenjaya Sep 01 '16 at 13:28
  • 1
    Arrays != pointers, An array is a contiguous block of memory, so `[10][10]` is one slab of memory, that holds 10 arrays of 10 `char *`. This means it's identical to a `char *[100]`, no idea where you're getting the idea of tripple indirection from, but whenver you see `***p`, step back and ask yourself it there's a better way. There's a reason why the term [three star programmer](http://c2.com/cgi/wiki?ThreeStarProgrammer) is not a compliment. – Elias Van Ootegem Sep 01 '16 at 13:49

4 Answers4

1

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] );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

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.

glglgl
  • 89,107
  • 13
  • 149
  • 217
0

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.
Lundin
  • 195,001
  • 40
  • 254
  • 396
0

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

jacob
  • 671
  • 6
  • 15