C中的数组递增运算符 [英] Array increment operator in C

查看:158
本文介绍了C中的数组递增运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白以下code的结果:

I don't understand the results of following code:

#include <stdio.h>
#include <conio.h>
int main()
{
   int a[4]={1, 3, 5, 6};
   //suppose a is stored at location 2010
   printf("%d\n", a + 2);
   printf("%d", a++);
   return 0;
}

为什么第二的printf 函数产生以下错误?

Why does the second printf function produce following error?

错误:左值要求作为增量的操作数

推荐答案

部分-1:

数组名是不变的(而不是修改的左值),您可以增值到数组名称,但不能修改它。

Array names are constant (not modifiable lvalue), your can add value to array name but can't modify it.

防爆pression A + 2 不修改 A 本身,而是当你做 A ++ 这是等同于 A = A + 1 尝试修改数组名--lvalue错误。这位前pression A ++ 在第二个printf是错误的 - 语义相位误差的一个例子。阅读以下语言标准:

Expression a + 2 doesn't modify a itself but when you do a++ that is equivalent to a = a + 1 try to modify array name --lvalue error. The expression a++ in second printf is wrong - an example of semantic phase error. read following language standards:

724 修改的左值是不具有数组类型的左值,
  没有一个不完整的类型,不具有常量限定
  类型,如果它是一个结构或联合,没有任何成员
  (包括递归,包含的任何成员或所有元素
  用const限定的类型的聚合或工会)。

6.3.2.1 Lvalues, arrays, and function designators

724 A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type, does not have a const-qualified type, and if it is a structure or union, does not have any member (including, recursively, any member or element of all contained aggregates or unions) with a const-qualified type.

729 除了当它是sizeof操作符的操作数,或一元&安培; 运营商,
  或者一个字符串用于初始化数组,一个前pression了
  已输入类型的数组转换为类型的前pression
  指针输入指向数组的初始元件
  对象为不是左值

729 Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type "array of type" is converted to an expression with type "pointer to type" that points to the initial element of the array object and is not an lvalue.

部分-2:

在最前pressions注意数组名衰变第一个元素的地址(读一些<一个href=\"http://stackoverflow.com/questions/17752978/exception-to-array-not-decaying-into-a-pointer/17753057#17753057\">exceptions其中,数组名不衰变成一个指向第一个元素?巧妙地回答@H 2 CO 3 )。

Note array names in most expressions decays in address of first element (read some exceptions where array name not decaying into a pointer to first element? ably answered by @H2CO3).

当你做 A + 2 其结果是第三个元素的地址(或索引元素的地址 2 )所以 A + 2 是一样的&功放;在指数[2] 这是地址没有价值。

When you do a + 2 its result is address of third element (or address of element at index 2) So a + 2 is same as &a[2] It is address not value at index.

要打印地址使用%P 而不是%d个和类型转换地址到无效* 如下:

To print address use %p instead of %d and typecast address into void* as follows:

printf("address (a + 2) = %p , &a[2] = %p", (void*)(a + 2), (void*)(&a[2]));

要打印值需要防守运营商 * 如下:

To print value you need defence operator * as follows:

printf("address *(a + 2) = %d , a[2] = %d", *(a + 2), a[2]);   

部分-3:

假设一个存储在2010的位置,首先是printf函数2012的输出?

suppose a is stored at location 2010, Is the output of first printf function 2012?

没有,指针运算不同,那么整数运算。我们知道数组名衰变成在最前pressions第一个元素的地址的地址,所以,当你做 A + 2 的值是第三个元素的地址,它是在指数 2 。因此,假设如果你的系统INT大小为4字节那么 A + 2 统计根据你的假设指向位置2018的 A 地址值是2010年。

No, pointer arithmetic is different then integer arithmetic. As we know array name decays into address of first element's address in most expressions So when you do a + 2 the value is address of third element that is at index 2. So suppose if int size in your system is 4 bytes then a + 2 stat pointing to location 2018 according to your assumption that a address value is 2010.

要了解阅读 10.2指针和数组;指针运算指针运算

这篇关于C中的数组递增运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆