Pointers on C——10 Structures and Unions.8

10.2.3 Accessing Structure Members


Now letʹs look at the arrow operator. The R‐value of the expression px->a is


The ‐> operator applies indirection to px (indicated by the solid arrow) in order to get the structure, and then selects the a member. The expression px->a is used when you have a pointer to a structure but do not know its name. If you knew the name of this structure, you could use the equivalent expression x.a instead.

>操作符对px 执行间接访问操作〈由实线箭头提示) ,它先得到它所指向的结构,然后访问成员a 。当你拥有个指向结构的指针但不知道构的名时,便可以使用表达式px-如果你知道这个结构的名,你也可以使用功能相同的表达式x.a 


Letʹs pause here and compare the expressions *px and px->a to each other. In both cases, the address in px is used to fold the structure. But the first member in the structure is a, so the address of a is the same as the address of the structure. It would seem, then, that px points to the structure and to the first member of the structure: after all, they both have the same address. This analysis is only half correct, though.Although both addresses have the same value, they have different types. The variable px was declared as a pointer to a structure, so the result of the expression *px is the whole structure, not its first member.

在此,我们稍作停顿,相互比较表达式*px 和px- >。在这两个表达式中, px 所保存的地址都用于找这个结构但结构的第1 个成员是a ,所以a 的地址和结构的地址是一样的这样px看上去是指向整个结构,同时指向结构的第1 个成:毕,它们具有相同的地址但是,这个分析只有半是正确的尽管两个地址的值是相等的,但它们的类型不同px 被声明为个指向构的指针,所以表达式*px 的结果是整个结构, 不是它的第1 个成员


Letʹs create a pointer to an integer.

让我们创建个指向整型的指针


int *pi;


Can we make pi point to the integer member a? If pi had the same value as px, then the result of the expression *pi would be the member a. But the assignment

我们能不能让pi 指向整型成员a? 如果pi 的值和px 相同,那么达式*pi 的结果将成员a 但是,表达式


pi = px;


illegal because their types do not match. Using a cast works,

是非法的,因为它们的类型不匹配使用强制类型转换就能奏效:


pi = (int *) px;


but is dangerous because it circumvents the compilerʹs type checking. The correct expression is simpler—to get a pointer to px->a, use the & operator:

但这种方法是很危险的,因为它避开了编译器的类型检查。正确的表达式更为简单——使用&操作符取得个指向px->a 的指针:


pi = &px->a;


The precedence of the -> operator is higher than that of &, so parentheses are not needed in this expression. Letʹs examine a diagram of &px->a:

->操作符的优先级高于&操作符的优先级,所以这个表达式无需使用括号让我们检查&px->a 的图:


Note how the value in the oval points directly to the a member of the structure, as opposed to px, which points to the entire structure. After the assignment above, pi and px will have the same value. But their types are different, so the result of applying indirection to them will also be different: *px is the whole structure, and *pi is a single integer.

注意椭圆里的值是如何直接指向结构的成员a 的,这与px 相反, 后者指向整个结构在上面的赋值操作之后, pi 和px 具有相同的值但它们的类型是不同的,所以对它们使用间接访问操作所得的结果也不样: *pX 的结果是整个结构, *pi 的结果是个单的整型值


Here is another example using the arrow operator. The value of px->b is a pointer constant because b is an array. This expression is not a legal L‐value. Here is its R‐value.

这里还有个使用箭头操作符的例子表达式px-b 的值是个指针常量,因为b 是个数组这个表达式不是个合法的左值下面是它的右值:



If we add indirection to this expression, it selects the first element of the array.With a subscript or pointer arithmetic, other elements of the array can be obtained as well. The expression px->b[1] selects the second array element, like this:

如果我们对这个表达式执行间接访问操作,它将访问数组的第1 个元素。使用下标引用或指针运算,我们还可以访问数组的其他元素表达式px>b[l] 访问数组的第2 个元素,如下所示:


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


Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9 Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Chapter 15 Chapter 16 Chapter 17 Chapter 18 Contents A Quick Start ........................................................................................................ 1 Basic Concepts ...................................................................................................... 7 Data ....................................................................................................................... 11 Statements ............................................................................................................. 15 Operators and Expressions .................................................................................... 23 Pointers .................................................................................................................. 29 Functions ............................................................................................................... 37 Arrays .................................................................................................................... 43 Strings, Characters, and Bytes .............................................................................. 55 Structures and Unions ........................................................................................... 69 Dynamic Memory Allocation ................................................................................ 75 Using Structures and Pointers ............................................................................... 79 Advanced Pointer Topics ...................................................................................... 87 The Preprocessor ................................................................................................... 93 Input/Output Functions .......................................................................................... 95 Standard Library .................................................................................................... 119 Classic Abstract Data Types ................................................................................. 129 Runtime Environment ........................................................................................... 145
Pointers On C brings the power of pointers to your C programs. Designed for professionals and advanced students, Pointers on C provides a comprehensive resource for those needing in-depth coverage of the C programming language. An extensive explanation of pointer basics and a thorough exploration of their advanced features allows programmers to incorporate the power of pointers into their C programs. Complete coverage, detailed explanations of C programming idioms, and thorough discussion of advanced topics makes Pointers on C a valuable tutorial and reference for students and professionals alike. Features and Benefits Provides complete background information needed for a thorough understanding of C. Covers pointers thoroughly, including syntax, techniques for their effective use and common programming idioms in which they appear. Compares different methods for implementing common abstract data structures. Offers an easy, conversant writing style to clearly explain difficult topics, and contains numerous illustrations and diagrams to help visualize complex concepts. Includes Programming Tips, discussing efficiency, portability, and software engineering issues, and warns of common pitfalls using Caution! Sections. Describes every function on the standard C library. For those who need an up-to-date ANSI overview of the C programming language, this book would be an excellent introduction. Pointers are usually a stumbling block for those programming C initially, but the author does an excellent job of detailing the use of pointers in this book. The use of pointers dominates the entire book, and after studying it, readers will have a thorough, practical knowledge of how to take advantage of the performance power of C language, due mostly to its use of pointers. For those programming in a commercial/business environment, where coding practices are strictly enforced, this book would be a good desk reference, as the author includes discussion of sound programming practices throughout the book. The book would also serve well those involved in teaching C in the classroom, as it contains many exercises, ranging from very easy to highly advanced. And for those readers frequently facing legacy code in C, such as scientific programmers, the author cites the differences between the older "Kernighan-Ritchie" C, and the more modern ANSI C, the latter being used in the book. These differences are indicated in the margin of the book, and are of an enormous help for those who must take older code and get it to run on more up-to-date compilers. The author also endeavors to organize the C code for those who are going on to study C++ and the accompanying object-oriented approach to programming. In addition, he emphasizes how to write C code so as to make it more portable. For those writing commercial applications in C that must be used on different platforms, this is a very important issue of course. Particularly well-written is the author's discussion on the storage class of a variable, noting, for those such as I who are pre-disposed to using recursion, that the formal parameters to a function cannot be static if recursion is to be supported. The book is full of examples such as this that give readers insight on the workings of C that fit their particular programming style. He does discuss `goto' statements in relation to function scope and in C statement structures, but, thankfully, recommends such statements never be used. He gives an interesting counterexample to those who say that goto statements must be used to break out of nested loops. Also, the author discusses the difference between L- and R-values, and this is not usually included in beginning books on C. Dynamic memory allocation has been at times a somewhat painful aspect of programming in C, but the author shows how to do straightforwardly in the book. Having a book like this that is predominantly about pointers is quite a blessing for those who are inexperienced with them or for more experienced programmers who are still uncomfortable with their use. It is not uncommon these days to have to write programs in one's professional work that involve triple pointers or even quadruple pointers. In addition, for embedded systems programming, the use of pointer arithmetic is almost mandatory. This also is true for writing applications in cryptography using C. The author does pay careful attention to pointer arithmetic in the book. The performance pay-off for using pointers is undeniable, and so a thorough knowledge of their use and pit-falls is of upmost importance for those C programmers who are involved in writing performance-sensitive applications. The author discusses in detail what can happen when pointers are misused and gives many examples of what to avoid and good hints for the proper use of pointers. He recommends against the use of the `null' pointer in array searching, and recommends a strategy for circumventing them. Some very helpful diagrams are given for explaining pointer expressions. In addition, the author gives helpful hints on when to use pointers and not subscripts when manipulating arrays in C. The performance issues involved in this are extremely important in scientific programming using C. The author gives a very interesting example of the differences in performance using pointers involving a program to copy the contents of one array into another. Arrays of pointers, useful in data mining applications, are also given ample treatment in this book, and the author addresses the issue of when to use a matrix instead of an array of pointers. The author also gives an effective presentation of functions in C, particularly the construction of recursive functions, and he employs some useful diagrams to illustrate how the variables in a recursive function call change on the stack. The performance hit experienced by using recursion versus iterative loops is discussed in a standard way via the Fibonacci series. Those readers raised in the functional programming paradigm will want to pay notice these performance issues when using C to do recursion. Along the same lines, the author shows how to implement functions with variable argument lists in C. This is another topic that is frequently passed over in beginning books on C. The author's treatment of data structures in C is also very nicely done, and he includes again a topic not usually treated in beginning books on C, namely the concept of a self-referential data structure. These are very important in applications in artificial intelligence, and the author shows how to implement them in C using a data structure that points to itself. This leads to a discussion of incomplete declarations. Very helpful diagrams are used again to discuss how to access members of data structures and how to point to data structures. Bit fields, so often used in embedded system applications, are also given a detailed treatment.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值