结构体

#include<iostream>
#include<cstring>
using namespace std;
typedef struct books{
char title[14];
char writer[14];

} books;
//void printbook(struct books *b);
void showbook(books *b){ //这里声明的同时定义函数体
cout<<b->title;
cout<<b->writer;//访问结构体指针执行的结构体的成员变量需要使用指向箭头

}
int main(){

books b1;
strcpy(b1.title,"hello");
strcpy(b1.writer,"webcyh");
showbook(&b1);

return 0;
}
~
"16.cpp" 23L, 324C written
[root@VM_0_11_centos gcc]# g++ 16.cpp -o 16.c
[root@VM_0_11_centos gcc]# ./16.c
hellowebcyh[root@VM_0_11_centos gcc]# 

  

您可以在上述定义的指针变量中存储结构变量的地址。为了查找结构变量的地址,请把 & 运算符放在结构名称的前面,如下所示:

struct_pointer = &Book1;

为了使用指向该结构的指针访问结构的成员,您必须使用 -> 运算符,如下所示:

struct_pointer->title;
原文地址:https://www.cnblogs.com/webcyh/p/11268085.html