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?

2
  • Welcome to SO. Please take the tour, read How to Ask and edit your post to include an minimal reproducible example. Commented Jan 27, 2021 at 23:27
  • Since these are strings it would perhaps be more convenient to use arrays of char* instead of 2D arrays. Commented Jan 28, 2021 at 9:00

1 Answer 1

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

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.