结构和联合
数据经常以成组的形式存在。如果这些值能够存储在一起,访问起来会简单一些。但是,如果这些值的类型不同,则无法存储于同一个数组中。在C中,使用结构可以把不同类型的值存储在一起。
10.1 结构基础知识
聚合数据类型(aggregate data type)能够同时存储一个以上的单独数据。C提供了两种类型的聚合数据类型:数组和结构。数组是相同类型的元素的集合,它的每个元素是通过下标引用或指针间接访问来选择的。
结构也是一些值的集合,这些值称为它的成员(member),但一个结构的各个成员可能具有不同的类型。
数组元素可以通过下标访问,这只是因为数组的元素长度相同。但是,在结构中情况并非如此。由于一个结构的成员可能长度不同,因此不能使用下标来访问它们。相反,每个结构成员都有自己的名字,它们是通过名字访问的。
这个区别非常重要。结构并不是一个它自身成员的数组。和数组名不同,当一个结构变量在表达式中使用时,它并不被替换成一个指针。结构变量也无法使用下标来选择特定的成员。
结构变量属于标量类型,所以可以像对待其他标量类型那样执行相同类型的操作。结构也可以作为传递给函数的参数,它们也可以作为返回值从函数返回;相同类型的结构变量相互之间可以赋值。可以声明指向结构的指针,取一个结构变量的地址,也可以声明结构数组。
/*
** 结构的基础知识。
*/
#include
#include
struct struct_a{
int i;
float f;
double d;
};
void print_struct_info( struct struct_a a );
struct struct_a update( struct struct_a a );
int main( void ){
int array[] = { 1, 2, 3, 4, 5, 6 };
int *p;
struct struct_a * pstruct;
struct struct_a exam_a;
struct struct_a exam_b;
struct struct_a exam_c;
int array_len;
int i;
/* the default length of array */
array_len = sizeof(array) / sizeof(*array);
/* use array name to represent the address of array directly */
p = array;
/* use & operator to obtain the address of examp_a */
pstruct = &exam_a;
printf( "access the elements of array by using the form of array[subscript] and print the size of every element:\n" );
for( i = 0; i < array_len; ++i ){
printf( "array[%d] = %d, sizeof(array[%d]) = %zd\n", i, array[i], i, sizeof(array[i]) );
}
printf( "\n" );
printf( "access the elements of array by using the form of pointer[subscript] and print the size of every element:\n" );
for( i = 0; i < array_len; ++i ){
printf( "p[%d] = %d, sizeof(p[%d]) = %zd\n", i, p[i], i, sizeof(p[i]) );
}
printf( "\n" );
/*
** in exam_a, the size of member may be different.
** Assign value to the member of struct.
*/
exam_a.i = 1;
exam_a.f = 1.0;
exam_a.d = 1.0;
print_struct_info( exam_a );
/*
** assignment operation of struct.
*/
exam_b = exam_a;
printf( "after exam_b = exam_b:\n" );
print_struct_info( exam_b );
/*
** struct type as return value of function and function parameter.
*/
exam_c = update( exam_b );
printf( "after update( exam_b ):\n" );
print_struct_info( exam_c );
/*
** declare the array of struct.
*/
struct struct_a struct_array[3];
struct_array[0] = exam_a;
struct_array[1] = exam_b;
struct_array[2] = exam_c;
char ch = 'a';
for( i = 0; i < 3; ++i ){
printf( "print the contents of exam_%c:\n", ch++ );
print_struct_info( struct_array[i] );
}
return EXIT_SUCCESS;
}
void print_struct_info( struct struct_a a ){
printf( "access the elements of struct by the form of var_name.member and print the size of every element:\n" );
printf( "var_name.i = %d, sizeof(var_name.i) = %zd\n", a.i, sizeof(a.i) );
printf( "var_name.f = %f, sizeof(var_name.f) = %zd\n", a.f, sizeof(a.f) );
printf( "var_name.d = %lf, sizeof(var_name.d) = %zd\n", a.d, sizeof(a.d) );
}
struct struct_a update( struct struct_a a ){
a.i = 2.0;
printf( "after var_name.i = 2.0:\n" );
return a;
}
/* 输出:
access the elements of array by using the form of array[subscript] and print the size of every element:
array[0] = 1, sizeof(array[0]) = 4
array[1] = 2, sizeof(array[1]) = 4
array[2] = 3, sizeof(array[2]) = 4
array[3] = 4, sizeof(array[3]) = 4
array[4] = 5, sizeof(array[4]) = 4
array[5] = 6, sizeof(array[5]) = 4
access the elements of array by using the form of pointer[subscript] and print the size of every element:
p[0] = 1, sizeof(p[0]) = 4
p[1] = 2, sizeof(p[1]) = 4
p[2] = 3, sizeof(p[2]) = 4
p[3] = 4, sizeof(p[3]) = 4
p[4] = 5, sizeof(p[4]) = 4
p[5] = 6, sizeof(p[5]) = 4
access the elements of struct by the form of var_name.member and print the size of every element:
var_name.i = 1, sizeof(var_name.i) = 4
var_name.f = 1.000000, sizeof(var_name.f) = 4
var_name.d = 1.000000, sizeof(var_name.d) = 8
after exam_b = exam_b:
access the elements of struct by the form of var_name.member and print the size of every element:
var_name.i = 1, sizeof(var_name.i) = 4
var_name.f = 1.000000, sizeof(var_name.f) = 4
var_name.d = 1.000000, sizeof(var_name.d) = 8
after var_name.i = 2.0:
after update( exam_b ):
access the elements of struct by the form of var_name.member and print the size of every element:
var_name.i = 2, sizeof(var_name.i) = 4
var_name.f = 1.000000, sizeof(var_name.f) = 4
var_name.d = 1.000000, sizeof(var_name.d) = 8
print the contents of exam_a:
access the elements of struct by the form of var_name.member and print the size of every element:
var_name.i = 1, sizeof(var_name.i) = 4
var_name.f = 1.000000, sizeof(var_name.f) = 4
var_name.d = 1.000000, sizeof(var_name.d) = 8
print the contents of exam_b:
access the elements of struct by the form of var_name.member and print the size of every element:
var_name.i = 1, sizeof(var_name.i) = 4
var_name.f = 1.000000, sizeof(var_name.f) = 4
var_name.d = 1.000000, sizeof(var_name.d) = 8
print the contents of exam_c:
access the elements of struct by the form of var_name.member and print the size of every element:
var_name.i = 2, sizeof(var_name.i) = 4
var_name.f = 1.000000, sizeof(var_name.f) = 4
var_name.d = 1.000000, sizeof(var_name.d) = 8
--------------------------------
Process exited after 0.09383 seconds with return value 0
请按任意键继续. . .
*/
10.1.1 结构声明
在声明结构时,必须列出它包含的所有成员。这个列表包括每个成员的类型和名字。
struct tag { member-list } variable-list ;
声明结构的语法需要做一些解释。所有可选部分不能全部省略---它们至少要出现两个。
这里有几个例子。
struct {
int a;
char b;
float c;
} x;
这个声明创建了一个名叫x的变量,它包含3个成员:一个整数、一个字符和一个浮点数。
struct {
int a;
char b;
float c;
} y[20], *z;
这个声明创建了y和z。y是一个数组,它包含了20个结构。z是一个指针,它指向这个类型的结构。
警告:
这两个声明被编译器当作两种截然不同的类型,即使它们的成员列表完全相同。因此,变量y和z的类型与x的类型不同,所以下面这条语句
z = &x;
是非法的。
这是不是意味着某种特定类型的所有结构都必须使用一个单独的声明来创建呢?
幸运的是,事实并非如此。标签(tag)字段允许为成员列表提供一个名字,这样它就可以在后续的声明中使用。标签允许多个声明使用同一个成员列表,并且创建同一种类型的结构。这里有个例子:
struct SIMPLE {
int a;
char b;
float c;
};
这个声明把标签SIMPLE和这个成员列表联系在一起。该声明并没有提供变量列表,所以它并未创建任何变量。
这个声明类似于制造一个甜饼切割器。甜饼切割器决定了做出来的甜饼的形状,但其本身却不是甜饼。标签标识了一种模式,用于声明未来的变量,但无论是标签还是模式,其本身都不是变量。
struct SIMPLE x;
struct SIMPLE y[20], *z;
这些声明使用标签来创建变量。它们创建和最初两个例子一样的变量,但存在一个重要的区别---现在x,y和z都是同一种类型的结构变量。
声明结构时可以使用的另一种良好技巧是用typedef创建一种新的类型,如下面的例子所示:
typedef struct {
int a;
char b;
float c;
} Simple;
这个技巧和声明一个结构标签的效果几乎相同。区别在于Simple现在是个类型名而不是个结构标签,所以后续的声明可能像下面这个样子:
Simple x;
Simple y[20], *z;
/*
** 结构声明。
*/
#include
#include
/*
** omit label of struct, create a variable.
*/
struct {
int a;
char b;
float c;
} x;
/*
** omit label of struct too, create a array y and a pointer z.
*/
struct {
int a;
char b;
float c;
} y[20], *z;
/*
** label of struct exits, omit the variable of type struct SIMPLE.
** It means it doesn't create any variable.
*/
struct SIMPLE {
int a;
char b;
float c;
};
/*
** omit the label of struct and variable, Simple is type of struct.
*/
typedef struct {
int a;
char b;
float c;
} Simple;
int main( void ){
/*
** can't pass compilation.
** because *z and x are annoymous struct, but they are the same data type.
** z = &x;
*/
/*
** use no label of struct to create three variables:
** x2 and x3 are two variables with the same type.
** y2 is a pointer points to struct { int a; char b; float c; } variable.
*/
struct {
int a;
char b;
float c;
} x2, x3, *y2;
y2 = &x2;
struct SIMPLE x;
struct SIMPLE y[20], *z;
/*
** can pass compilation.
** because *z and x are the same data type.
*/
z = &x;
Simple sx;
Simple sy[20], *sz;
sz = &sx;
return EXIT_SUCCESS;
}
提示:
如果想在多个源文件中使用同一种类型的结构,就应该把标签声明或typedef形式的声明放在一个头文件中。如果源文件需要这个声明,可以使用#include指令把那个头文件包含进来。
/*
** create three files: simple.h, simple.cpp, main.cpp
*/
/* the source code of simple.h is follow: */
/*
** put struct type declared by typedef into header file.
*/
#ifndef SIMPLE_H
#define SIMPLE_H
#include
#include
/*
** omit the label of struct and variable, Simple is type of struct.
*/
typedef struct {
int a;
char b;
float c;
} Simple;
void print_simple( void );
#endif
/* the source code of simple.cpp is follow: */
#include "simple.h"
void print_simple( void ){
Simple simple;
simple.a = 1;
simple.b = 'a';
simple.c = 1.0f;
printf( "simple.a = %d\n", simple.a );
printf( "simple.b = %c\n", simple.b );
printf( "simple.c = %f\n", simple.c );
}
the source code is follow:
#include "simple.h"
int main( void ){
print_simple();
return EXIT_SUCCESS;
}
/* 输出:
*/
10.1.2 结构成员
可以在一个结构外部声明的任何变量都可以作为结构的成员。尤其是,结构成员可以是标量、数组、指针甚至是其他结构。
struct COMPLEX {
float f;
int a[20];
long *lp;
struct SIMPLE s;
struct SIMPLE sa[10];
struct SIMPLE *sp;
};
一个结构的成员可以和其他结构的成员的名字相同,所以这个结构的成员a并不会与struct SIMPLE s的成员a冲突。正如接下去看到的那样,成员的访问方式允许指定任何一个成员而不至于产生歧义。
#include
#include
/*
** label of struct exits, omit the variable of type struct SIMPLE.
** It means it doesn't create any variable.
*/
struct SIMPLE {
int a;
char b;
float c;
};
struct COMPLEX {
float f;
int a[20];
long *lp;
struct SIMPLE s;
struct SIMPLE sa[10];
struct SIMPLE *sp;
};
int main( void ){
return EXIT_SUCCESS;
}
10.1.3 结构成员的直接访问
结构变量的成员是通过点操作符(.)访问的。点操作符接受两个操作数:左操作数就是结构变量的名字;右操作数就是需要访问的成员的名字。这个表达式的结果就是指定的成员。例如:
struct COMPLEX comp;
名字为a的成员是一个数组,所以表达式comp.a就选择了这个成员。这个表达式的结果是个数组名,所以可以把它用在任何可以使用数组名的地方。尤其是,我们可以把这个表达式用作另一个点操作符的左操组数,如(comp.s).a,用来选择结构comp的成员s(也是一个结构)的成员a。点操作符的结合性是从左向右,所以可以省略括号,表达式comp.s.a表示同样的意思。
这里有一个更为复杂的例子。成员sa是一个结构数组,所以comp.sa是一个数组名,它的值是一个指针常量。对这个表达式使用下标引用操作,如(comp.sa)[4]将选择一个数组元素。但这个元素本身是一个结构,所以可以使用另一个点操作数取得它的成员之一。下面就是一个这样的表达式:
( (comp.sa)[4] ).c
下标引用和点操作符具有相同的优先级,它们的结合性都是从左向右,所以可以省略所有的括号。下面的表达式和前面那个表达式是等价的。
comp.sa[4].c
/*
**结构成员的直接访问
*/
#include
#include
/*
** label of struct exits, omit the variable of type struct SIMPLE.
** It means it doesn't create any variable.
*/
struct SIMPLE {
int a;
char b;
float c;
};
struct COMPLEX {
float f;
int a[20];
long *lp;
struct SIMPLE s;
struct SIMPLE sa[10];
struct SIMPLE *sp;
};
int main( void ){
struct COMPLEX comp;
int *pa;
int a;
SIMPLE simple;
float c, c2;
pa = comp.a;
a = comp.s.a;
simple = (comp.sa)[4];
c = (comp.sa)[4].c;
printf( "c = %f\n", c );
c2 = comp.sa[4].c;
printf( "c2 = %f\n", c2 );
return EXIT_SUCCESS;
}
/* 输出:
*/
10.1.4 结构成员的间接访问
如何使用一个指向结构的指针访问结构的成员呢?首先就是对指针执行间接访问操作,从而获得这个结构。然后再使用点操作符来访问它的成员。但是,点操作符的优先级高于间接访问操作符,所以必须在表达式中使用括号,确保间接访问首先执行。例如,假定一个函数的参数是个指向结构的指针,如下面的原型所示:
void func( struct COMPLEX *cp );
函数可以使用下面这个表达式来访问这个变量所指向的结构的成员f:
(*cp).f
对指针执行间接访问将访问结构,然后点操作符访问一个成员。
由于这个结构有些惹人厌,因此C语言提供了一个更为方便的操作符来完成这项工作--- ->操作符(也称箭头操作符)。和点操作符一样,箭头操作符接受两个操作数,但左操作数必须是一个指向结构的指针。箭头操作符对左操作数执行间接访问取得指针所指向的结构,然后和点操作符一样根据右操作数选择一个指定的结构成员。但是,间接访问操作内建于箭头操作符中,所以不需要显示地执行间接访问或使用括号。例如,它们像前面一样使用同一个指针。
cp->f 访问结构的浮点数成员
cp->a 访问结构的一个数组名
cp->s 访问结构的一个结构
/*
**结构成员的间接访问。
*/
#include
#include
/*
** label of struct exits, omit the variable of type struct SIMPLE.
** It means it doesn't create any variable.
*/
struct SIMPLE {
int a;
char b;
float c;
};
struct COMPLEX {
float f;
int a[20];
long *lp;
struct SIMPLE s;
struct SIMPLE sa[10];
struct SIMPLE *sp;
};
void func( struct COMPLEX *cp );
int main( void ){
struct COMPLEX comp;
struct COMPLEX *pcomp;
pcomp = ∁
func( pcomp );
return EXIT_SUCCESS;
}
void func( struct COMPLEX *cp ){
float f, f2;
int *p, *p2;
struct SIMPLE simple, simple2;
f = cp->f;
f2 = (*cp).f;
p = cp->a;
p2 = (*cp).a;
simple = cp->s;
simple2 = (*cp).s;
}
/* 输出:
*/
10.1.5 结构的自引用
在一个结构内部包含一个类型为该结构本身的成员是否合法呢?这里有一个例子,可以说明这个想法:
struct SELF_REF1 {
int a;
struct SELF_REF1 b;
int c;
};
这种类型的自引用是非法的,因为成员b是另一个完整的结构,其内部还将包含它自己的成员b。这第二个成员又是另一个完整的结构,它还将包括它自己的成员b。这样重复下去永无止境。这有点像永远不会终止的递归程序。但下面这个声明却是合法的,你能看出其中的区别吗?
struct SLEF_REF2 {
int a;
struct SLEF_REF2 *b;
int c;
};
这个声明和前面那个声明的区别在于b现在是一个指针而不是结构。编译器在结构的长度确定之前就已经知道指针的长度,所以这种类型的自引用是合法的。
如果你觉得一个结构内部包含一个指向该结构本身的指针有些奇怪,请记住它事实上所指向的同一种类型的不同结构。更加高级的数据结构,如链表和树,都是用这种技巧实现的。每个结构指向链表的下一个元素或树的下一个分枝。
警告:
警惕下面的陷阱:
typedef struct {
int a;
SELF_REF3 *b;
int c;
} SELF_REF3;
这个声明的目的是为这个结构创建类型名SELF_REF3,但是它失败了。类型名直到声明的末尾才定义,所以在结构声明的内部它尚未定义。
解决方案是定义一个结构标签来声明b,如下所示:
typedef struct SELF_REF3_TAG {
int a;
struct SELF_REF3_TAG *b;
int c;
} SELF_REF3;
/*
** 结构成员的间接访问。
*/
#include
#include
/*
** can't pass compilation.
** because it can lead to loop infinitely.
** struct SELF_REF1 {
int a;
struct SELF_REF1 b;
int c;
** };
*/
/*
** can pass compilation.
** because struct SLEF_REF2 can be konwed.
*/
struct SLEF_REF2 {
int a;
struct SLEF_REF2 *b;
int c;
};
/*
** can't pass compilation.
** because we can't the definition of SELF_REF3 when the program stream executes it.
** typedef struct {
int a;
SELF_REF3 *b;
int c;
** } SELF_REF3;
*/
/*
** a solution.
*/
typedef struct SELF_REF3_TAG {
int a;
struct SELF_REF3_TAG *b;
int c;
} SELF_REF3;
int main( void ){
return EXIT_SUCCESS;
}
/* 输出:
*/
10.1.6 不完整的声明
有时候,我们必须声明一些相互之间存在依赖的结构。也就是说,其中一个结构包含了另一个结构的一个或多个成员。和自引用结构一样,至少有一个结构必须在另一个结构内部以指针的形式存在。问题在于声明部分:如果每个结构都引用了其他结构的标签,应该首先声明哪个结构呢?
这个问题的解决方案是使用不完整声明(incomplete declaration),它声明一个作为结构标签的标识符。然后可以把这个标签用在不需要知道这个结构的长度的声明中,如声明指向这个结构的指针。接下来的声明把这个标签与成员列表联系在一起。
考虑下面这个例子,两个不同类型的结构内部都有一个指向另一个结构的指针:
struct B;
struct A {
struct B *partner;
/* other declarations */
};
struct B {
struct A *partner;
/* other declarations */
};
在A的成员列表中需要标签B的不完整声明。一旦A被声明之后,B的成员列表也可以被声明。
/*
** 不完整的声明。
*/
#include
#include
/*
** member in struct A is a pointer points to the type of struct B,
** member in struct B is a pointer points to the type of struct A.
*/
struct B;
struct A {
struct B *partner;
/* other declarations */
};
struct B {
struct A *partner;
/* other declarations */
};
/*
** member in struct A2 is a variable of the type of struct B2,
** member in struct B2 is a pointer points to the type of struct A2.
*/
struct B2 {
struct A2 *partner;
/* other declarations */
};
struct A2 {
struct B2 partner;
/* other declarations */
};
int main( void ){
return EXIT_SUCCESS;
}
/* 输出:
*/
10.1.7 结构的初始化
结构的初始化方式和数组的初始化方式很相似。一个位于一对花括号内部、由逗号分隔的初始值列表可用于结构中各个成员的初始化。
这些值根据结构成员列表的顺序写出。如果初始列表的值不够,剩余的结构成员将使用缺省值进行初始化。
结构中如果包含数组或结构成员,其初始化方式类似于多维数组的初始化。一个完整的聚合类型成员的初始化列表可以嵌套于结构的初始化值列表内部。例如:
struct INIT_EX {
int a;
short b[10];
Simple c;
} x = {
10,
{ 1, 2, 3, 4, 5 },
{ 25, 'x', 1.9 }
};
/*
** 结构的初始化。
*/
#include
#include
/*
** omit the label of struct and variable, Simple is type of struct.
*/
typedef struct {
int a;
char b;
float c;
} Simple;
struct INIT_EX {
int a;
short b[10];
Simple c;
} x = {
10,
{ 1, 2, 3, 4, 5 },
{ 25, 'x', 1.9 }
};
int main( void ){
int i;
int arr_len;
printf( "print the content of x:" );
printf( "x.a = %d\n", x.a );
arr_len = sizeof(x.b) / sizeof(*x.b);
for( i = 0; i < arr_len; ++i ){
printf( "x.b[%d] = %d\n", i, x.b[i] );
}
printf( "x.c.a = %d, x.c.b = %c, x.c.c = %f\n", x.c.a, x.c.b, x.c.c );
return EXIT_SUCCESS;
}
/* 输出:
*/