请求成员的东西不是结构或工会?

时间:2021-08-31 00:34:51

I have the following code:

我有以下代码:

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

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

The person declaration is Person *people[]

个人声明是人*人[]

I am receiving an error: request for member 'personName' in something not a structure or union

我接收到一个错误:请求成员的“personName”,而不是结构或union。

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

But i am not sure what is wrong with the program.

但是我不确定这个程序有什么问题。

Thanks

谢谢

3 个解决方案

#1


2  

I assume people is a Person * or Person[].

我认为人是人或人[]。

When using operator ->, you dereference and access the variable. It is equivalent of doing (*var).personName.

当使用操作符->时,您取消引用并访问变量。它相当于做(*var). personname。

Change the -> with a dot, as when using person[1], you already dereference your pointer, and then access your variable like you would do with a simple Person var

使用一个点来改变->,当使用person时,您已经取消了指针,然后像使用一个简单的person var那样访问变量。

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

#2


0  

Use . instead of -> since you are using an array of structures.

使用。而不是->,因为你使用的是一系列结构。

#3


0  

The declaration of a type needs to be done prior to accessing the (members of an) instance of a type.

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

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

...

Person *people[];

...

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

#1


2  

I assume people is a Person * or Person[].

我认为人是人或人[]。

When using operator ->, you dereference and access the variable. It is equivalent of doing (*var).personName.

当使用操作符->时,您取消引用并访问变量。它相当于做(*var). personname。

Change the -> with a dot, as when using person[1], you already dereference your pointer, and then access your variable like you would do with a simple Person var

使用一个点来改变->,当使用person时,您已经取消了指针,然后像使用一个简单的person var那样访问变量。

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

#2


0  

Use . instead of -> since you are using an array of structures.

使用。而不是->,因为你使用的是一系列结构。

#3


0  

The declaration of a type needs to be done prior to accessing the (members of an) instance of a type.

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

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

...

Person *people[];

...

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