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?

2
  • 1
    I think you will get some idea from here stackoverflow.com/questions/13554244/… Commented Sep 1, 2016 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 is not a compliment. Commented Sep 1, 2016 at 13:49

4 Answers 4

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] );
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.