结构体成员指针变量赋值
时间: 2023-12-18 19:27:59 浏览: 114
结构体指针
5星 · 资源好评率100%
结构体成员指针变量赋值的方法如下所示:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int iAge;
char szAddr1[100];
char *pszAddr2;
char **pszAddr3;
} T_PeopleInfo;
int main() {
T_PeopleInfo people;
char *pszAddr2 = "Beijing";
char *pszAddr3 = "China";
people.iAge = 18;
strcpy(people.szAddr1, "Shanghai");
people.pszAddr2 = pszAddr2;
people.pszAddr3 = &pszAddr3;
printf("Age: %d\n", people.iAge);
printf("Address 1: %s\n", people.szAddr1);
printf("Address 2: %s\n", people.pszAddr2);
printf("Address 3: %s\n", *(people.pszAddr3));
return 0;
}
```
在上述代码中,我们定义了一个结构体T_PeopleInfo,其中包含了4个成员变量,其中3个是指针类型。我们通过定义指针变量pszAddr2和pszAddr3,将其分别赋值给结构体成员变量people.pszAddr2和people.pszAddr3。在输出时,我们使用printf函数输出结构体成员变量的值。
阅读全文