繁体   English   中英

在函数声明中引用Struct类型?

[英]Referring to Struct types in a function declaration?

如果我声明一个简单的结构,如下所示:

typedef struct {    
  char name[50];    
  int age;    
} Person;    

struct Person people[7];

然后在下面参考它以插入数据:

static void insert(Person people[HOW_MANY], char *name, int age)    
{    
  static int nextfreeplace = 0;    

  people[nextfreeplace].name = &name;    
  people[nextfreeplace].age = age;     

  nextfreeplace += 1;    
}    

我收到一个不兼容的类型错误:

error: incompatible types when assigning to type 'char[50]' from type 'char **' people[nextfreeplace].name = &name;

我是否声明了我的结构错误? 还是我弄乱了指针?

只需使用

snprintf(people[nextfreeplace].name, 50, "%s", name);

复制字符串。 在这种情况下,它还会检查缓冲区大小。

暂无
暂无

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

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