#include<stdlib.h>
#include<stdio.h>
#include<string.h>
// 结构体指针
struct Book{
char *name;
char *author;
int page;
};
struct Computer {
char *name;
int price;
double weight;
};
int main() {
// 结构体数组
struct Book books[2] ={
{"西游记", "吴承恩", 100}, {"水浒传", "施耐庵", 200}};
// 遍历结构体数组
// 第一种遍历方式 数组遍历
int i= 0;
for (; i< 2; i++){
printf("数组遍历1 book name is %s, author is %s, page is %d\n", books[i].name, books[i].author, books[i].page);
}
// 第二种遍历方式 指针遍历 在Windows上有问题
i = 0;
struct Book *pBooks = &books[0];
// for (; pBooks < pBooks+1; pBooks++) {
// printf("指针遍历2 book name is %s, author is %s\n", pBooks->name, pBooks->author);
// }
for (; i <sizeof(books)/sizeof(struct Book); i++) {
printf("指针遍历2 book name is %s, author is %s, page is %d\n", pBooks->name, pBooks->author, pBooks->page);
pBooks++;
}
007-结构体数组遍历
最新推荐文章于 2023-11-09 14:22:14 发布
本文通过一个C语言示例介绍了如何定义结构体并使用三种不同方法遍历结构体数组,包括数组遍历、指针遍历及再次数组遍历的方式。


6449

被折叠的 条评论
为什么被折叠?



