1

Hi im trying to access strings stored in 2 different arrays using an array of pointers using the code:


typedef char (*p)[2][20]; //pointer to array of strings


    ///create dictionaries
    char names[2][20] = {"jack","john"};
    char items[2][20] = {"car"};
    
    p pointer[2]= {&names,&items};
    
    print(pointer[x][y])

replacing x and y with x=1 y=0 prints car, while x=0 y=0 prints jack but im not sure how to print john. using x=0 y=1 prints ( @x, whilst x=1 y=1 also prints jack and x=1 y=2 also prints ( @x. I was wondering how to access john?

JAck
  • 15
  • 2
  • Welcome to SO. Please take the [tour], read [ask] and [edit] your post to include an [mcve]. – jwdonahue Jan 27 '21 at 23:27
  • Since these are strings it would perhaps be more convenient to use arrays of `char*` instead of 2D arrays. – Lundin Jan 28 '21 at 09:00

1 Answers1

1

pointer[x] is a pointer to an array.

You need to dereference it to get the array of strings, then index into that.

printf("%s\n", (*pointer[x])[y]);
Barmar
  • 741,623
  • 53
  • 500
  • 612