#include <stdio.h>
#include <string.h>
//结构体的定义
struct BOOK {
char id[4]; //编号
char name[64]; //书名
float price; //单价
};
int main(void)
{
//结构体也可以初始化时直接赋值
struct BOOK clang = {"101", "劝学网的C语言教程", 25.5};
struct BOOK books[2];
struct BOOK *p;
//变量或数组对结构体元素的赋值
strcpy(books[0].id, "201");
strcpy(books[0].name, "劝学网的Oracle教程");
books[0].price = 35.5;
//指针对结构体元素的赋值
p = books + 1;
strcpy(p->id, "202");
strcpy(p->name, "小雅的Java教程");
p->price = 45.5;
//显示结构体变量的内容
printf("%s, %s, %f\n", clang.id, clang.name, clang.price);
printf("%s, %s, %f\n", books[0].id, books[0].name, books[0].price);
printf("%s, %s, %f\n", p->id, books[1].name, (books+1)->price);
return 0;
}
|