简体   繁体   中英

Size of pointer array inside Struct

I have these 2 structs :

typedef struct {
    char name[20];
    int num;
    int score;
} player;

typedef struct {
    char name[20];
    player *players;
} team;

I need to know the amount of elements stored per every *players inside my team struct. Thanks!

You can't. players is a pointer to N player instances. It carries no more information than an address. It is not an array; it is a pointer.

You will have to store the number of elements in the struct separately.

typedef struct {
    char name[20];
    player *players;
    size_t num_players;
} team;

On a side note, you had better hope that Jarrod Saltalamacchia never joins this team.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM