gpt4 book ai didi

可以在嵌套 for 循环中使用双括号吗?

转载 作者:行者123 更新时间:2023-11-30 21:42:15 26 4
gpt4 key购买 nike

我的问题的意思是,如果我有一个像这样的嵌套for循环

for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 10; i++)
{
printf("%d\n"___);
}
}

我该在空白处填什么?如果我已经声明了一个数组,[i][j] 会非法吗?

最佳答案

根据你的问题,我不确定你到底被困在什么地方,所以我做了一个带有注释的最小C程序

我声明了一个 int 数组,其第一维和第二维至少为 10,因为您从 0 到 9 迭代 ij (包括的)。这是为了避免迭代时出现越界问题

数组的元素未在程序中初始化。当您运行该程序时,它可能会打印全零。它也有可能打印内存中恰好存在的其他值(因为数组值未初始化)

最后我在 for 循环之外声明了 ij 以防万一这是您面临的问题

#include <stdio.h>

int main(int argc, char** argv) {
// Declare an array
// Note that both dimensions are at least 10
// Also note that none of the values are initialized here
int myArray[10][10];

// Both i and j are declared here rather than in the for loop
// This is to avoid the following potential error:
// error: 'for' loop initial declarations are only allowed in C99 or C11 mode
int i, j;

for (i = 0; i < 10; i++) {
for (j = 0; j < 10; j++) {
// Note that the values this prints are uninitialized
printf("%d\n", myArray[i][j]);
}
}

return 0;
}

关于可以在嵌套 for 循环中使用双括号吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30316364/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com