C和指针---第十章：结构和联合
C和指针---第十章：结构和联合
fzyz_sb 发表于5年前
C和指针---第十章：结构和联合
• 发表于 5年前
• 阅读 59
• 收藏 0
• 评论 0

10.1 结构基础知识

10.1.1 结构声明

``struct tag { member-list } variable-list;``

``````struct {
int		a;
char	b;
float	c;
} x;``````

``````struct {
int    a;
char	b;
float	c;
} y[20], *z;``````

``z = &x;``

``````struct SIMPLE {
int	a;
char	b;
float	c;
};``````

``````struct SIMPLE x;
struct SIMPLE y[20], *z;``````

``````typedef struct {
int	a;
char	b;
float	c;
} Simple;``````

``````Simple x;
Simple y[20], *z;``````

10.1.2 结构成员

``````struct COMPLEX{
float	f;
int	a[20];
long	*lp;
struct SIMPLE	a;
struct SIMPLE	sa[10];
struct SIMPLE	*sp;
};``````
10.1.3 结构成员的直接访问

10.1.4 结构成员的间接访问

10.1.5 结构的自引用

``````struct SELF_REF1{
int		a;
struct	SELF_REF1 b;
int		c;
};``````

``````struct SELF_REF1{
int		a;
struct	SELF_REF2 *b;
int		c;
};``````

``````typedef struct {
int		a;
struct	SELF_REF3 *b;
int		c;
} SELF_REF3;``````

``````typedef struct SELF_REF3_TAG {
int		a;
struct	SELF_REF3_TAG *b;
int		c;
} SELF_REF3;``````
10.1.6 不完整的声明

``````struct B;

struct A {
struct B	*partner;
};

struct B {
struct A	*partner;
};``````
10.1.7 结构的初始化

``````struct INTF_EX {
int		a;
short	b[10];
Simple  c;
}x = {
10,
{
1,2,3,4,5
},
{
25, 'x', 1.9
}
};``````
10.2 结构，指针和成员
``````typedef struct {
int	a;
short	b[2];
} Ex2;
typedef struct Ex{
int	a;
char	b[3];
Ex2	c;
struct	Ex	*d;
} Ex;

Ex x = {10, "Hi", { 5, { -1, 25 }, }, 0 };
Ex *px = &x;``````

10.2.2 访问结构

10.2.3 访问结构成员

*px和px->a。它们有什么区别？

1. 它们具有相同的地址，所以*px == px->a。

2. 但是，它们类型是不同的！！！变量px被声明为一个指向结构的指针，所以表达式*px的结果是整个结构，而不是它的第一个成员。

``int *pi;``

``pi = &px->a;``
10.2.4 访问嵌套的结构

``*px->c.b;``

10.2.5 访问指针成员

``px->d;``

``*px->d;``

``````Ex    y;
x.d = &y;
px->d->c.b[1];``````

10.4 作为函数参数的结构

``````typedef struct {
char	product[PRODUCT_SIZE];
int	quantity;
float	unit_price;
float	total_amount;
} Transaction;``````

``````void print_receipt( Transcation trans ){
printf("%s\n", trans.product);
printf("%d @ %.2f total %.2f\n", trans.quantity, trans.unit_price, trans.total_amount );
}``````

``````void print_receipt( Transcation *trans ){
printf("%s\n", trans->product);
printf("%d @ %.2f total %.2f\n", trans->quantity, trans->unit_price, trans->total_amount );
}``````

10.6 联合

``````#include <stdio.h>

int main(void)
{
union {
float	f;
int	i;
} fi;

fi.f = 3.14159;
printf("%d\n", fi.i );
printf("%d\n", (int)fi.f );

return 0;
}``````

10.6.2 联合的初始化

``````union {
int	a;
float	b;
char	c[4];
} x = { 5 };``````

1.

``````#define MAX_SIZE 128
typedef struct {
long	num1;
long	num2;
long	num3;
}Num;
struct inf{
char	date[MAX_SIZE];
char	time[MAX_SIZE];
struct	telephoneNumber
{
Num		yourNum;
Num		otherNum;
Num		payNum;
};
};``````

2.

``````typedef struct Money{
float	msrp;
float	asp;
float	st;
float	lf;
}Money;

typedef struct Sales{
float	msrp;
float	asp;
float	dp;
float	sd;
float	mp;
int	lt;
}Sales;

typedef struct RunSales{
float	msrp;
float	asp;
float	st;
float	lf;
float	dp;
int	ld;
float	ir;
float	mp;
char	nob[20];
};

struct inf{
char	name[20];
union model{
Money		moneyModel;
Sales		salesModel;
RunSales	runsalesModel;
};
};``````

×