如果我声明一个如下所示的简单结构:
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);
复制字符串。在这种情况下,它还会检查缓冲区大小。
关于c - 在函数声明中引用结构类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40376355/