c语言+二维数组

发布于 2025-01-07 10:36:06 字数 340 浏览 3 评论 0原文

我有一个 C 代码。 我给出的数组索引为 12。但它允许我将数组初始化为该索引,而不是给出索引越界的错误。 任何人都可以向我解释一下它正在发生吗?

int vas1[12][12];
    vas1[15][15]=0;

    int i,j;
    for (i = 0; i < 15; i ++)
    {
        for (j = 0; j < 15; j ++) {
            printf("i=%d j=%d vas=%d",i,j,vas1[i][j]); 

        }
    }

printf("Success");

谢谢

I am having one c code.
Where i had given an array index as 12.But it is allowing me to initialize the array more to that index instead of giving error for index out of bound.
Can any one please explain me y it is happeining.

int vas1[12][12];
    vas1[15][15]=0;

    int i,j;
    for (i = 0; i < 15; i ++)
    {
        for (j = 0; j < 15; j ++) {
            printf("i=%d j=%d vas=%d",i,j,vas1[i][j]); 

        }
    }

printf("Success");

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随心而道 2025-01-14 10:36:06

C 不对数组访问进行边界检查。它只是将非法访问标记为“未定义的行为”,因此每个实现都可以按照自己的意愿行事。由于使用 C 意味着您知道自己在做什么,因此 C 允许您 搬起石头砸自己的脚

在实践中,有时会出现错误,有时不会。有时您不会收到错误,但客户端会收到错误。最坏的情况:您不会收到错误,但程序的行为真的很奇怪(变量无缘无故地改变值等)。

C doesn't do bounds checking on array accesses. It simply marks illegal accesses as "undefined behavior" so each implementation can do as it please. Since using C means you know what you're doing, C allows you to shoot yourself in the foot.

In practice, sometimes you will get an error, sometimes not. Sometimes you won't get an error but the client will. Worst case scenario: you won't get an error but the program will behave really weird (variables changing values for no reason etc).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文