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

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

问题描述

我不明白以下代码的结果:

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
", a + 2);
   printf("%d", a++);
   return 0;
}

为什么第二个printf函数会产生如下错误?

Why does the second printf function produce following error?

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

推荐答案

Part-1:

数组名称是常量(不可修改的左值),您可以为数组名称添加值但不能修改它.

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

表达式 a + 2 不会修改 a 本身,但是当你做 a++ 时,它等价于 a = a +1 尝试修改数组名--lvalue 错误.第二个 printf 中的表达式 a++ 是错误的 - 语义阶段错误的一个例子.阅读以下语言标准:

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 限定类型,如果是结构体或联合体,则没有任何成员(包括,递归地,所有包含的任何成员或元素聚合或联合)具有 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 运算符或一元 & 运算符的操作数,or 是一个用于初始化数组的字符串文字,一个表达式具有类型类型数组"被转换为具有类型的表达式指向数组初始元素的类型指针"对象并且不是左值.

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 部分:

注意大多数表达式中的数组名称在第一个元素的地址中衰减(阅读一些数组名称不会衰减为指向第一个元素的指针的例外情况?@H2CO3 巧妙回答).

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&a[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 并将地址类型转换为 void* 如下:

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 部分:

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

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

不,指针算术不同于整数算术.正如我们所知,在大多数表达式中,数组名称会衰减为第一个元素的地址,因此当您执行 a + 2 时,值是索引 2 处的第三个元素的地址.因此,假设您的系统中的 int 大小为 4 个字节,则 a + 2 stat 根据您的假设 a 地址值为 2010 指向位置 2018.

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天全站免登陆