- 声明结构的形式:
//第一种,p1 和 p2 都是point里面有x和y的值
struct point{
int x;
int y;
};
struct point p1,p2;
//第二种,一次性使用;p1 和 p2都是一种无名结构,里面有x和y
struct{
int x;
int y;
}p1,p2;
//第三种,最常用;
struct point{
int x;
int y;
}p1,p2;
其中第三种注意区别用typedef的结构:
typedef struct point{
int x;
int y;
}p3;
p3 a;//这时p3是代表struct point{ int x;int y;}
所以这里的a其实与上述第三种的p1都是结构变量;
- 结构的初始化:用大括号
struct date today = {07,13,2014};
struct date thismonth ={.month=7,.year=2014};
- 结构运算:
p1 = (struct point){5, 10};
// 相当于p1.x = 5;
p1.y = 10;
p1 = p2;
// 相当于p1.x = p2.x; p1.y = p2.y;
数组无法做这两种运算!
- 结构指针:
和数组不同,结构变量的名字并不是结构变量的地址,必须使用&运算符
struct date *pDate = &today;
- 指向结构的指针:
struct date{
int month;
int day;
int year;
}myday;
struct date *p =&myday;
//以下两行相等,第二行是缩写,方便,注意只有指向结构的指针才有这种缩写。
(*p).month =12;
p->month =12 ;

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



