1. declared and defined structure
Structure: collection of different types of variable data;
First definition method (most commonly used):
#include<stdio.h>
struct weapon{
char name[20];
int atk;
int price;
};//一定别忘了这个冒号;
int main{
struct weapon weapon_1={"weapon_name",100,200};
return 0;
}
The second is defined by:
#include<stdio.h>
struct weapon{
char name[20];
int atk;
int price;
}weapon_1; //相当于定义了一个全局变量,变量的类型是struct weapon,变量的名称是weapon_1;
int main{
return 0;
}
This structure is not clearly written, is not conducive to the maintenance program;
The third-defined method:
struct{
char name[20];
int atk;
int price;
}waapon_1;
Such writing can not be defined other structure variable name;
2. Initialization structure and references
Initialization and cited general structure:
struct weapon weapon_1={"weapon_name",100,200};
printf("%s\n%d\n",weapon_1.name,++weapon_1.price);
Initialize an array of structures and references:
struct weapon weapon_2[2]={{"weapon_name1",100,50},{"weapon_name2",100,200}};//里面的花括号也可以不加;
printf("%s\n%d\n",weapon_2[0].name,weapon_2[1].atk);
3. The structure pointer
Structure variable pointer:
struct weapon* w;
w=&weapon_1;
printf("name =%s\nname =%s\n",(*w).name,w->name);//same result;
Structure pointer array variables:
struct weapon* p;
p = weapon_2;
printf("%s\n",p->name);
p++;// equal weapon_2 +1 指向weapon_2[1];
printf("%s\n",p->name);