我在一个 .h 文件中有三个结构。
typedef struct name{
char first[20];
char middle;
char last[20];
} PersonName;
typedef struct address{
char street[20];
char city[20];
char state[20];
int zipCode;
} PersonAddress;
typedef struct employee{
PersonName bob;
PersonAddress place;
int id;
} Employee;
我正在尝试做的是使用员工结构来完成其他结构的工作。
我有一个电话:
void setName(struct employee* object, char* f, char* l, char* m);
到我的 .c 文件,该文件应该在以下位置接收:
void setName(struct employee* object, char* f, char* l, char* m){
strncpy(object.bob.first,f,20);
strncpy(object.bob.last,l, 20);
object.bob.middle=m[0];
}
能够在不使用其他结构的情况下更改值。我只是看错了吗? CMD 说“请求 bob 不是结构。 任何帮助将不胜感激。
最佳答案
Bob 是一个指针。为了访问其成员,请尝试使用 -> 运算符而不是 .
换句话说:
void setName(struct employee* object, char* f, char* l, char* m){
strncpy(object->bob.first,f,20);
strncpy(object->bob.last,l, 20);
object->bob.middle=m[0];
}
关于c - 在其他结构中使用结构。 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23506083/