简体   繁体   English

Struct内部的指针数组的大小

[英]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. 我需要知道team结构中每个*players存储的元素数量。 Thanks! 谢谢!

You can't. 你不能 players is a pointer to N player instances. players是一个指针到N player实例。 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. 从侧面讲,您最好希望Jarrod Saltalamacchia永远不要加入这个团队。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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