#include <stdio.h>
#include <stdlib.h>
//三种结构体类型的定义方式,
struct weapon1{
char name[20];
int atk;
float price;
};
struct weapon2{
char name[20];
int atk;
float price;
}weapon2;
//相当于struct weapon weapon2;但是这样写结构不够清晰,不便于维护
struct{
char name[20];
int atk;
float price;
}weapon3;
//省略结构体类型,这样无法使用该结构体去定义一个新的结构体变量
int main()
{
//结构体变量的声明
//struct weapon1 weapon01;
//结构体变量的初始化,
struct weapon1 weapon02 = {"weapon_1", 100, 200};
//访问结构体,使用点运算符,所有运算符中优先级最高,结构体也可以用运算,如四则,自加自减,
printf("%s\n, %d\n",weapon02.name,weapon02.atk++);
//结构体数组,
struct weapon1 weapon03[2] = {{"weapon_1", 10, 20},{"weapon_2", 200, 200}};
printf("%s\n, %d\n",weapon03[0].name,weapon03[1].atk);
//结构体指针,
struct weapon1 * w;
w = &weapon02;
//printf("%s\n, %d\n",(*w).name,(*w).atk);
printf("%s\n, %d\n",w->name,w->atk);
//使用指针访问结构体数组,
struct weapon1 * p;
p = &weapon03;
printf("%s\n, %d\n",p->name,p->atk);
p++;//相当于weapon03 +1, 指向weapon03[1]
printf("%s\n, %d\n",p->name,p->atk);
return 0;
}