Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
110 views
in Technique[技术] by (71.8m points)

c - Array of pointers to array of strings, can only access first element of array

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?

question from:https://stackoverflow.com/questions/65928804/array-of-pointers-to-array-of-strings-can-only-access-first-element-of-array

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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
", (*pointer[x])[y]);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...