数组中的元素太多! [英] Too many elements in an array!

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

问题描述

对不起,如果这是一个noob问题:(。

Sorry if this is a noob question :( .

一段C代码。

int array[5];
int cnt;

for(cnt = 0; cnt <= 10; cnt+=1)
{
      array[cnt] = cnt;
}

应该给出错误但是为什么会这样呢?似乎在第一行 - 定义了超过双倍大小(11)的数组,甚至可以稍后再访问数组[5到10] 。这让我感到困惑,当你定义数组[4或更少] ...

Should give an error, right? No! Works fine! But why is that? It seems that -in the first line- an array of more than the double size (11) is defined. You can even access array[5 to 10] later on. And that is confusing me. It stops working when you define array[4 or less] ...

提前谢谢。

推荐答案

可能会与您的特定编译器和计算机配合使用,但不要指望。

It may happen to work with your particular compiler and computer, but you shouldn't count on it.

根据C语言规范,您的代码的行为是未定义,这意味着它可能会执行您希望的操作,或者可能导致您的计算机崩溃,否则可能导致

The behaviour of your code according to the C language specification is undefined. This means that it might do what you hope, or it might cause your computer to crash, or it might cause demons to fly out your nose.

与Java和C#等高级语言不同,C信任你,不执行显式检查在数组的界限上。你应该负责,不要超越阵列的边界。

Unlike higher-level languages such as Java and C#, C trusts you and does not perform explicit checks on the bounds of arrays. You are supposed to be responsible and not tread outside the boundaries of the array.

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

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