题目描述
[md]
编写代码
```cpp
// 结构体嵌套的例子
#include
#include
using namespace std;
struct Address {
char city[20]; // 城市
char street[120]; // 街道
int zip; // 邮编
};
struct Student{
int num;
char name[20];
Address addr;
};
int main(){
Student s1 = {1, "张三", {"北京", "长安街", 100000}};
Student s2;
s2.num = 2;
strcpy(s2.name,"李四");
strcpy(s2.addr.city,"南京");
strcpy(s2.addr.street,"北京西路");
s2.addr.zip = 200210;
return 0;
}
```
```cpp
#include
using namespace std;
struct School{
char s_name[120];//学校
int s_age;
}s1 = {"ASchool",100}, s2 = {"BSchool",50};
// 结构体变量作为函数参数
void print_a(School s){
printf("%s %d\n", s.s_name, s.s_age);
s.s_age++;
}
// 结构体指针作为函数参数
void print_b(School *sp){
printf("%s %d\n", sp->s_name, sp->s_age);
sp->s_age++;
}
// 结构体指针作为函数参数
void swap(School *a,School *b){
School temp;
temp = *a;
*a = *b;
*b = temp;
}
// 结构体数组作为函数参数
void print_all(School a[],int n){
for(int i=0;i
using namespace std;
struct Book{
int number;
char name[32];
int price;
};
void print(Book *b, int price){
for(int i=0;i<5;i++){
if(b->price >= price)
printf("%d %s %d\n",b->number,b->name,b->price);
b++;
}
// for(int i=0;i<5;i++){
// if(b[i].price >= price)
// printf("%d %s %d\n",b[i].number,b[i].name,b[i].price);
// }
}
//void list(Book b[], int price){
// for(int i=0;i<5;i++){
// if(b[i].price >= price)
// printf("%d %s %d\n",b[i].number,b[i].name,b[i].price);
// }
//}
int main(){
Book b[5]={{1,"santi",25},{2,"hongloumeng",45},{3,"jinpingmei",50},{4,"xiyouji",87},{5,"sanguo",67}};
print(b,50);
return 0;
}
```
[/md]