请求成员不是结构或联盟?

时间:2012-10-25 09:23:25

标签: c arrays pointers

我有以下代码:

people[nextfreeplace] -> personName = name;
people[nextfreeplace] -> age = age;

typedef struct person
{
  char *personName;
  int age;
}Person;

此人声明为Person *people[]

我收到了error: request for member 'personName' in something not a structure or union

error: request for member 'age' in something not a structure or union

但我不确定该计划有什么问题。

由于

3 个答案:

答案 0 :(得分:2)

我认为peoplePerson *Person[]

使用运算符->时,您可以取消引用并访问该变量。这相当于做(*var).personName

使用点更改->,就像使用人[1]时一样,您已取消引用指针,然后像使用简单的Person var

一样访问变量
people[nextfreeplace].personName = name;
people[nextfreeplace].age = age;

答案 1 :(得分:0)

使用.代替->,因为您使用的是结构数组。

答案 2 :(得分:0)

需要在访问类型的(成员)实例之前完成类型声明。

typedef struct person
{
  char *personName;
  int age;
}Person;

...

Person *people[];

...

people[nextfreeplace] -> personName = name;
people[nextfreeplace] -> age = age;