为什么for循环在2D数组中出现溢出时不起作用,但在1D数组中仍然起作用

huangapple go评论106阅读模式
英文:

Why for loop doesn't work for 2D array with an overflow but still works for 1D array

问题

以下是翻译好的部分:

"The for loop to store values to two 1D arrays can store and print values even with an overflow but why a program to store values to 2D array using for loop doesn't run when there is an overflow?"

"用for循环将值存储到两个一维数组中,即使溢出,也可以存储和打印值,但为什么使用for循环将值存储到二维数组的程序在溢出时不运行?"

"The following program runs:"

"以下程序可以运行:"

#include <stdio.h>

int main()
{
 int i, a[5], b[6];
 for (i = 0; i <= 6; i++)
 {
    a[i] = i;
    b[i] = i;
 }

 for (i = 0; i <= 6; i++)
 {
   printf(" %d", a[i], b[i]);
 }
    
 return 0;
}

"But why the following program doesn't run?"

"但为什么以下程序不运行?"

#include <stdio.h>

int main()
{
int i, j, a[5][5];

for (i = 0; i <= 5; i++)
{
    for (j = 0; j <= 5; j++)
    {
        a[i][j] = j;
    }
}


for (i = 0; i <= 5; i++)
{
    for (j = 0; j <= 5; j++)
    {
        printf(" %d", a[i][j]);
    }
printf("\n");
}

return 0;
}

"I tried to store the values in 2D array using nested for loop but it doesn't run. However, the program runs when 1D array is used."

"我尝试使用嵌套的for循环将值存储在二维数组中,但它不运行。然而,当使用一维数组时,程序可以运行。"

英文:

The for loop to store values to two 1D arrays can store and print values even with an overflow but why a program to store values to 2D array using for loop doesn't run when there is an overflow?

The following program runs:

#include &lt;stdio.h&gt;

int main()
{
 int i, a[5], b[6];
 for (i = 0; i &lt;= 6; i++)
 {
    a[i] = i;
    b[i] = i;
 }

 for (i = 0; i &lt;= 6; i++)
 {
   printf(&quot; %d&quot;, a[i], b[i]);
 }

 return 0;
}

But why the following program doesn't run?

#include &lt;stdio.h&gt;

int main()
{
int i, j, a[5][5];

for (i = 0; i &lt;= 5; i++)
{
    for (j = 0; j &lt;= 5; j++)
    {
        a[i][j] = j;
    }
}


for (i = 0; i &lt;= 5; i++)
{
    for (j = 0; j &lt;= 5; j++)
    {
        printf(&quot; %d&quot;, a[i][j]);
    }
printf(&quot;\n&quot;);
}

return 0;
}

I tried to store the values in 2D array using nested for loop but it doesn't run. However, the program runs when 1D array is used.

答案1

得分: 0

第一个你分享的代码有效,因为你很幸运。当你说 int a[5] 时,这意味着你分配了 5 个整数单元,从零到四开始。不包括五。

修正后的第一个代码:

#include <stdio.h>

int main()
{
 int i, a[5], b[6];
 for (i = 0; i <= 4; i++)
 {
    a[i] = i;
    
 }
 for (i = 0; i <= 5; i++)
 {
    b[i] = i;
    
 }

 for (i = 0; i <= 5; i++)
 {
   if(i<5)
     printf("a= %d\nb= %d", a[i], b[i]);
   else
     printf("b=%d",b[i]);
 }

 return 0;
}

对于第二个代码,同样不应该尝试使用边界。

修正后的第二个代码:

#include <stdio.h>

int main()
{
int i, j, a[5][5];

for (i = 0; i < 5; i++)
{
    for (j = 0; j < 5; j++)
    {
        a[i][j] = j;
    }
}


for (i = 0; i < 5; i++)
{
    for (j = 0; j < 5; j++)
    {
        printf(" %d", a[i][j]);
    }
printf("\n");
}

return 0;
}
英文:

First one that you shared works because you are lucky. When you say
int a[5] this means you allocated 5 unit for integer and it starts from zero to four. It does not include five.

Fixed code first one:

#include &lt;stdio.h&gt;

int main()
{
 int i, a[5], b[6];
 for (i = 0; i &lt;= 4; i++)
 {
    a[i] = i;
    
 }
 for (i = 0; i &lt;= 5; i++)
 {
    b[i] = i;
    
 }

 for (i = 0; i &lt;= 5; i++)
 {
   if(i&lt;5)
     printf(&quot;a= %d\nb= %d&quot;, a[i], b[i]);
   else
     printf(&quot;b=%d&quot;,b[i]);
 }

 return 0;
}

for the second again you shouldn't try to use boundaries
Fixed code:

#include &lt;stdio.h&gt;

int main()
{
int i, j, a[5][5];

for (i = 0; i &lt; 5; i++)
{
    for (j = 0; j &lt; 5; j++)
    {
        a[i][j] = j;
    }
}


for (i = 0; i &lt; 5; i++)
{
    for (j = 0; j &lt; 5; j++)
    {
        printf(&quot; %d&quot;, a[i][j]);
    }
printf(&quot;\n&quot;);
}

return 0;
}

huangapple
  • 本文由 发表于 2023年7月23日 19:22:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747974.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定