如何访问数组[-1]位置的元素? [英] How to access a element at array[-1] position ?

查看:129
本文介绍了如何访问数组[-1]位置的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int a[10];
    int i;
    for (i=0; i<10; i++)
      a[i] = i;
    printf("%d", a[-1]);
    return 0;

推荐答案

0。你已经在数组外的-1位置访问数组了。

1.不要这样做。

2.无论你想做什么原因这有更好的方法。

3.查看数字1.

...

...

-1。砰!你的程序已经死了。
0. You are already accessing the array at -1 position which is outside the array.
1. Don''t do this.
2. Whatever the reason for wanting to do this there is a better way to do it.
3. See number 1.
...
...
-1. BANG! your program is dead.


你不能,如果你的意思是最后一个元素那么你必须做 a [(sizeof(a)/ sizeof (a [0]) - 1]
You can''t, if you mean the last but one element then you have to do a[(sizeof(a) / sizeof(a[0])-1].


是的,只有当位置索引更大时才可以访问数组元素[i-1]然后是0(在从零开始的数组中)。



Yes, you can access to an array element [i-1] only if the index of position is bigger then 0 (in zero-based arrays).

int i = 0;
int j = 0;
for (i=0; i<10; i++)
{
    printf("%d", a[i]);
    if (i>0)
    {
    j=i-1;
    printf("%d", a[j]);
    }
}


这篇关于如何访问数组[-1]位置的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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