C / C ++:用INT I [3],我为什么[2]是一样的2 [I]? [英] C/C++: with int i[3], why i[2] is the same as 2[i]?

查看:134
本文介绍了C / C ++:用INT I [3],我为什么[2]是一样的2 [I]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只知道这一招。

在C ++:

 的#include<&iostream的GT;
使用命名空间std;诠释主(){
    INT I [3] = {5,7,11};
    COUT<< 2 [I]
    返回0;
}

在C:

 的#include<&stdio.h中GT;
INT主要(无效)
{
    INT I [3] = {5,7,11};
    的printf(%d个,2 [I]);
    返回0;
}

是的,这是允许的,两个输出是11。

我为什么[2]可以前pressed为2 [I]?


解决方案

  A [i] = I [A]*(A + I)= *(1 + A)的

因此​​都是相同的,是有效的。

I just know this trick.

In C++:

#include <iostream>
using namespace std;

int main() {
    int i[3]={5,7,11};
    cout << 2[i];
    return 0;
}

In C:

#include <stdio.h>
int main(void) 
{  
    int i[3]={5,7,11};
    printf("%d", 2[i]);
    return 0; 
}

yes, these are allowed, both output are 11.

why i[2] can be expressed as 2[i]?

解决方案

a[i] = i[a]

*(a+i) = *(i+a)

Hence both are same and is valid.

这篇关于C / C ++:用INT I [3],我为什么[2]是一样的2 [I]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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