C99标准学习笔记(3)——操作符&和[]

本文详细解析了C语言中数组与指针的基本概念及其相互转换原理,通过实例演示了如何使用数组下标和指针来访问数组元素,并探讨了取地址运算符(&)的用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天的笔记没有一个很好的标题,也很短。主要是针对&与[]操作符
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 *= 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值