2

I have 2-D array

char arr[2][3]={"sam","ali"}

and pointer to this array

char(*ptr)[3]=arr;

How can I use this pointer to print arr[2][2] which in this case is i. I've tried * (*(ptr+1)+2) the same way I am dealing with the array but didn't work so can any one help and tell me how to deal with pointer to array in this case to print element [2][2].

Bence Kaulics
  • 6,700
  • 7
  • 31
  • 61
  • Avoid multi-dimensional arrays in C. Consider using [flexible array members](https://en.wikipedia.org/wiki/Flexible_array_member) to implement your own matrix abstract data type, like [here](https://stackoverflow.com/a/41410503/841108) – Basile Starynkevitch Aug 16 '17 at 06:58
  • 2
    Be careful when using strings and fixed-sized arrays, you have to remember that a string of three characters really need space for *four* characters, to include the terminating null character `'\0'`. – Some programmer dude Aug 16 '17 at 06:58
  • 1
    Note that `"sam"` actually requires 4 bytes, as it includes the string termination character – Stephan Lechner Aug 16 '17 at 06:59

3 Answers3

7

You should not print arr[2][2] because you are accessing array out of bounds. Also note that there is no space for '\0' character to be stored in in the given array.

char arr[2][4] = {"sam","ali"}  
char(*ptr)[4] = arr;

Using ptr you can access the elements of arr as ptr[1][2].

haccks
  • 100,941
  • 24
  • 163
  • 252
3

This:

char(*ptr)[3]=arr;

isn't a pointer to a multi dimensional array, it's a pointer to one-dimensional array of size 3. But that's fine because a pointer can always also point to an array of the type it points to. So you have ptr point to an array of one-dimensional arrays of size 3. So far just for clarifying the terms.

Your immediate problem are just wrong indices. Indices are based on 0, not 1, so with

char arr[2][3]={"sam","ali"}

valid indices for the first dimension of arr are just 0 and 1. The element you're looking for would be at ptr[1][2].


With the pointer arithmetics notation in your question, you actually had the indices right, so I can't see where your problem was in this case. The following prints i as expected:

#include <stdio.h>

int main(void)
{
    char arr[2][3]={"sam","ali"};
    char(*ptr)[3]=arr;
    printf("%c\n", *(*(ptr+1)+2));
}

Note this is completely equivalent to the more readable ptr[1][2].


Side note: if you expected your elements to be strings -- they aren't, see haccks' answer for the explanation (a string must end with a '\0', your string literals do, but your array needs the room to hold it).

0
char arr[2][3]={"sam","ali"}

is desugared into

char arr[2][3]={{'s','a','m'},
                {'a','l','i'}}

Next,

char(*ptr)[3]=arr;

means a pointer to an array of 3 elements of type char.

arr[2][2] is undefined because you did not initialize it.

However, if you wanted to select the element i that means arr[1][2], which is desugared into *(*(arr+1)+2). This can be expressed as ((ptr+1)+2) because the sizeof(*ptr) is the same as the sizeof(*arr).

alinsoar
  • 14,813
  • 4
  • 53
  • 68