c - 在函数声明中引用结构类型?

标签 c arrays pointers struct

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

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/

相关文章:

c - 功能和性能的返回值

c - 为信号量定义变量

c++ - 如何创建抽象类的动态数组?

c++ - vector 插入和分配之间的区别

Java 的表现就像我有指针一样。为什么?

c - 试图找到在两个数组中重复的数字

c++ - 尝试实现 HeapSort

c - 使用 while 和 do-while 循环在 C 中输出简单的猜数游戏

javascript - 在 javascript 中使用 map 更改数组

arrays - 数组如何存储在堆栈中?