gpt4 book ai didi

c - for 循环内计数器的奇怪行为

转载 作者:行者123 更新时间:2023-11-30 21:35:44 25 4
gpt4 key购买 nike

我注意到 for 循环的计数器(a变量)效果不佳。事实上,计数器并没有正确递减;我知道这个问题看起来很愚蠢,但我不明白为什么 a 变量会这样做。

#include <stdio.h>

int main() {
int a,i,b,matrice[2][2];
printf("Put inside the matrix some numbers..\n");


for (a=2;a>=0;a--) {
for (b=2;b>=0;b--) {
matrice[a][b]=scanf("%d",&i);
}
}

return 0;
}

最佳答案

当您读入数组时,您就超出了范围。

for (a=2;a>=0;a--) {

数组只有两个元素,但您尝试在第一次迭代时读入第三个元素。

matrice[2]

是第三个元素。这对于两个维度都会发生。


scanf() 调用不正确。它将值读取到i中,但返回的是读取的项数,而不是i的值。

关于c - for 循环内计数器的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26576417/

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