Pointers on C——10 Structures and Unions.3

10.1.2 Structure Members

In the examples so far, I have used only simple types for members. But any kind of variable that can be declared outside of a structure may also be used as a structure member. Specifically, structure members can be scalars, arrays, pointers, and even other structures.Here is a more complex example:

到目前为止的例子里,我只使用了简单类型的结构成员。但可以在一个结构外部声明的任何变量都可以作为结构的成员。尤其是,结构成员可以是标量、数组、指针甚至是其他结构。这里有一个更为复杂的例子:



The members of a structure may have names that are identical to members of other structures, so the member a of this structure doesnʹt conflict with the member a which is a part of the struct SIMPLE s. As you will see next, the way in which members are accessed allows either member to be specified without ambiguity.

一个结构的成员的名字可以和其他结构的成员的名字相同,所以这个结构的成员a 并不会与struct SIMPLE s 的成员a 冲突。正如你接下去看到的那样,成员的访问方式允许你指定任何一个成员而不至于产生歧义。


10.1.3 Direct Member Access

The members of a structure variable are accessed with the dot operator, which takes two operands. The left operand is the name of a structure variable and the right operand is the name of the desired member. The result of this expression is the designated member. For instance, consider the declaration

结构变量的成员是通过点操作符(.)访问的。点操作符接受两个操作数,左操作数就是结构变量的名字,右操作数就是需要访问的成员的名字。这个表达式的结果就是指定的成员。例如,考虑下面这个声明


struct COMPLEX comp;


The member named a is an array, so the expression comp.a selects that member. This expression is an array name, so you can use it in any context in which an array name could be used. Similarly, the member s is a structure, so the expression comp.s is the name of a structure variable and may be used in any way that we might use an ordinary structure variable. Specifically, we can use this expression as the left operand of another dot operator, as in (comp.s).a, to select the member named a of the structure s, which is a member of the structure comp. The dot operator associates from left to right so we do not need the parentheses; the expression comp.s.a is equivalent.

名字为a 的成员是一个数组,所以表达式comp.a 就选择了这个成员。这个表达式的结果是个数组名,所以你可以把它用在任何可以使用数组名的地方。类似地,成员s 是个结构,所以表达式comp.s的结果是个结构名,它可以用于任何可以使用普通结构变量的地方。尤其是,我们可以把这个表达式用作另一个点操作符的左操作符,如((comp.s).a选择结构comp 的成员s (也是一个结构)的成员a。点操作符的结合性是从左向右,所以我们可以省略括号,表达式comp.s.a 表示同样的意思。


Here is a more complex example. The member sa is an array of structures, so comp.sa is an array name whose value is a pointer constant.Applying a subscript to this expression, as in (comp.sa)[4], yields one element of the array. But this element is a structure, so we can use another dot operator to get one of its members. Here is the expression:

这里有一个更为复杂的例子。成员sa 是一个结构数组,所以comp.sa 是一个数组名,它的值是一个指针常量。对这个表达式使用下标引用操作,如(comp.sa)[4]将选择一个数组元素。但这个元素本身是一个结构,所以我们可以使用另一个点操作符取得它的成员之一。下面就是一个这样的表达式:


( (comp.sa)[4] ).c


The subscript and dot operators have the same precedence and all associate left to right, so we donʹt need these parentheses either. The expression comp.sa[4].c is equivalent.

下标引用和点操作符具有相同的优先级,它们的结合性都是从左向右,所以我们可以省略所有的括号。下面的表达式comp.sa[4].c和前面那个表达式是等效的。

上一章 Pointers on C——10 Structures and Unions.2


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值