10.2 Structures, Pointers, and Members
The operators for accessing structures and their members directly and through pointers are quite simple, but they can become confusing when applied in complicated situations. Here are some examples to help you better understand how the operators work. The examples use the following declarations.
直接或通过指针访问结构和它们的成员的操作符是相当简单的,但是当它们应用于复杂的情形时就有可能引起混淆。这里有几个例子,能帮助你更好地理解这两个操作符的工作过程。这些例子使用了下面的声明。
typedef struct {
int a;
short b[2];
} Ex2;
typedef struct EX {
int a;
char b[3];
Ex2 c;
struct EX *d;
} Ex;
Structures of type Ex will be pictured like this:
类型为EX 的结构可以用下面的图表示:
The structures are pictured this way to make the examples clearer. In fact, the diagram is not completely accurate, because the compiler avoids wasting space between the members whenever it can.
我用图的形式来表示结构,使这些例子看上去更清楚一些。事实上,这张图并不完全准确,因为编译器只要有可能就会设法避免成员之间的浪费空间。
The first examples will use these declarations:
第1 个例子将使用这些声明:
Ex x = { 10, "Hi", {5, { -1, 25 } }, 0 };
Ex *px = &x;
which produce the following variables:
它将产生下面这些变量:
We will now examine and diagram different expressions using the notation from Chapter 6.
我们现在将使用第6 章的记法研究和图解各个不同的表达式。