本文介绍了为什么'c [& i]'编译而'c [i]'不编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此示例在编译时没有警告/错误(gcc 4.8.2 -Wall):

This example compiles without warnings/errors (gcc 4.8.2 -Wall):

#include <stdio.h>
int main()
{
    char c;
    int i;
    printf("%p %02x\n",&i,c[&i]);
    printf("%p %02x\n",&c,c[&c]);

    // important to note that this line DOESN'T compile:
    //printf("%p %02x\n",&i,c[i]);
    //  which makes sense as c is NOT an array, it is a char.

    return 1;
}

为什么编译语法c [& i]?是故意的还是意外的?
语法c [& i]在语义上是否有效? (它有任何有用的意思吗?)

Why does the syntax c[&i] compile ? Is it intentional or an accident?Is the syntax c[&i] semantically valid ? (Does it have any useful meaning?)

输出示例:(指针每次都会改变)

Example of output for me: (pointers change each time)

0xbfb2577c b7718cea
0xbfb2577b 08

这个问题由来来自此处有问题的奇怪代码 ch2 [& i]:

This question originated from a strange bit of code 'ch2[&i]' in question here:C duplicate character,character by character

注意NOTE#0(更新后,经过反思)关于重复项/类似问题:
这个问题是不是c数组引用上的链接问题的重复项。它是相关的,因此引用它们很有用。相关问题讨论了有效的a [b]和b [a]情况,其中a或b中的一个是指针,另一个是int。如果a或b中的一个为char,则此问题的处理更为怪异,并且可能无效。
对于C数组,为什么会出现a [5] == 5 [a]? 14个答案

作为数组索引的字符串3回答

NOTE#0 (updated, upon reflection) on duplicates/similar questions:This question is not a duplicate of the linked questions on c array references. It is related so it is useful to refer to them. The related questions discuss the valid a[b] and b[a] case where one of a or b is a pointer and the other is an int. This question deals with the more weird and maybe should be invalid case where one of a or b is a char.With C arrays, why is it the case that a[5] == 5[a]? 14 answersWith arrays, why is it the case that a[5] == 5[a]?String as an array index 3 answersString as an array index

注意#1:这种情况发生在编译器作为变量类型的情况下c是char,与指针结合使用时可以用作数组的索引。

NOTE #1: This happens with compiler as the type of variable c is char and that can be used as index into array when combined with pointer.

注意#2:由于某种原因, c [ < ptr>] 计算为< ptr> 的类型。

NOTE #2: for some reason, type of c[<ptr>] evaluates to type of <ptr>.

例如: c [& pi] c [& pc] 的结果类型在以下代码中引起警告:

E.g.: the resulting types of c[&pi] and c[&pc] cause warnings in the following code:

int *pi; char *pc; pi=&i; pc=&c;
printf("%p %02x\n",&pi,c[&pi]);
printf("%p %02x\n",&pc,c[&pc]);

int *或 char *类型的警告,而不是 unsigned int类型:

Warnings on type 'int *' or 'char *' instead of 'unsigned int':

c/so_cweirdchar2.c: In function ‘main’:
c/so_cweirdchar2.c:13:5: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 3 has type ‘int *’ [-Wformat=]
     printf("pi %p %02x\n",&pi,c[&pi]);
     ^
c/so_cweirdchar2.c:14:5: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 3 has type ‘char *’ [-Wformat=]
     printf("pc %p %02x\n",&pc,c[&pc]);
     ^


推荐答案

在C中, [] 后缀指针替换运算符是可交换的。如果 a [i] 是有效的表达式,那么 i [a] 也是有效的表达式,它的含义相同。尽管我们必须注意操作员的关联性和优先级,以确保我们正确执行此逆转操作。您的 c [& i] (& i)[c] 含义相同:指针& i 被整数 c 取代。

In C, the [] postfix pointer displacing operator is commutative. If a[i] is a valid expression, then so is i[a] and means the same thing. Though we have to watch operator associativity and precedence to make sure we do this reversal right. Your c[&i] means the same thing as (&i)[c]: the pointer &i displaced by the integer c.

[] 的可交换性的基础是索引以及指针移位和解引用的组合之间的等价关系,其中 + 运算符是可交换的:

The basis for the commutativity of [] is the equivalence between indexing and the combination of pointer displacement and dereferencing, where the + operator is commutative:

E1[E2]  <-->  *(E1 + E2)
  ^               ^
  |               |
  v               v
E2[E1]  <-->  *(E2 + E1)

在这里我们理解 E1 为完全带括号的表达式,因此我们不关心优先级。实际上,我们正在操纵以 E1 E2 为节点的抽象语法树。

Here we understand E1 to be fully parenthesized expressions, so we are unconcerned about precedence. In effect we are manipulating abstract syntax trees in which E1 and E2 are nodes.

这篇关于为什么'c [&amp; i]'编译而'c [i]'不编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 03:50
查看更多