Chinaunix首页 | 论坛 | 博客
  • 博客访问: 832476
  • 博文数量: 157
  • 博客积分: 542
  • 博客等级: 中士
  • 技术积分: 1696
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-21 20:21
文章分类
文章存档

2017年(1)

2016年(2)

2015年(6)

2014年(42)

2013年(77)

2012年(19)

2011年(10)

分类:

2011-12-01 09:53:45

本文的copyleft归gfree.wind@gmail.com所有,使用GPL发布,可以自由拷贝,转载。但转载请保持文档的完整性,注明原作者及原链接,严禁用于任何商业用途。
作者:gfree.wind@gmail.com
博客:linuxfocus.blog.chinaunix.net

今天的笔记没有一个很好的标题,也很短。主要是针对&与[]操作符
A postfix expression followed by an expression in square brackets [] is a subscripted
designation of an element of an array object. The definition of the subscript operator []
is that E1[E2] is identical to (*((E1)+(E2))). Because of the conversion rules that
apply to the binary + operator, if E1 is an array object (equivalently, a pointer to the
initial element of an array object) and E2 is an integer, E1[E2] designates the E2-th
element of E1 (counting from zero).
也就是说对于数组array[10],实际上其等同于*((array)+(10)),也就等于10[array]。

The unary & operator yields the address of its operand. If the operand has type "type", the result has type "pointer to type". If the operand is the result of a unary * operator, neither that operator nor the & operator is evaluated and the result is as if both were omitted, except that the constraint on the operators still apply and the result is not an lvalue. Similarly, if the operand is the result of a [] operator, neither the & operator nor the unary * that is implied by the [] is evalued and the result is as if & operator were removed and the [] operator were changed to a + operator. Otherwise, the result is a pointer to the object or function designated by its operand.
也就说在*&NULL完全是合法的。

请看测试程序:
  1. #include <stdlib.h>
  2. #include <stdio.h>

  3. int main()
  4. {
  5.     int *p = NULL;
  6.     int array[10] = {1,2,3,4,5,6,7,8,9,0};

  7.     printf("p is %p and value is %d\n", &*p, 2[array]);

  8.     return 0;
  9. }
输出:
  1. p is (nil) and value is 3

阅读(1126) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~