相关疑难解决方法(0)

没有出界错误

我在C中有这个代码,它接受了一堆chars

#include<stdio.h> 
# define NEWLINE '\n'
int main()
{

char c;
char str[6];
int i = 0;
while( ((c = getchar()) != NEWLINE))
{
        str[i] = c;
        ++i;
        printf("%d\n", i);
}

return 0;
}
Run Code Online (Sandbox Code Playgroud)

输入是:testtesttest

输出:1 2 3 4 5 6 7 8 117 118 119 120

我的问题是:

  1. 虽然我明显超出了阵列的容量,为什么我不会出现超出界限(分段错误)的异常?

  2. 为什么输出中的数字会突然跳到很大的数字?

我在C++中试过这个并且得到了相同的行为.请问有谁可以解释一下这是什么原因?

c indexoutofboundsexception

12
推荐指数
2
解决办法
2万
查看次数

标签 统计

c ×1

indexoutofboundsexception ×1