为什么 for 循环使用超出范围的值?
why the for loop is using out of range values?
在下面的代码中,循环必须在第 4 个值处结束,因为它是循环的结尾,但取而代之的是,它开始使用甚至不遵循循环条件的值,即 100,甚至不是也停止..当我放一个 printf 语句来测试它然后我发现它在 a[i] 中使用 100 这是不可能的..
#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
int n,i,m;
i=0;
char a[i];
printf("how many characters you want to enter=");
scanf("%d",&n);
for (i=1;i<=n;i++)
{
scanf(" %c",&a[i]);
printf("%d\n",i);
//now this loop is not stopping//
}
for (i=1;i<=n;i++)
{
printf("%c \t",a[i]);
}
}
输出:-
how many characters you want to enter=4
a
1
b
2
c
3
d
100
e
101
i=0;
char a[i];
是undefined behavior so wrong. You should allocate an automatic variable which is a non-empty array, or preferably use carefully C dynamic memory allocation
使用前阅读Modern C, see this C reference, the documentation of your C compiler (e.g. GCC...) and of your debugger (e.g. GDB...). Be sure to read the documentation of every standard function (e.g. of scanf
)。请注意 scanf
可能会失败,并且 returns 是一个您应该测试的有趣整数。
如果您使用 GCC,请编译 with 所有警告和调试信息,因此 gcc -Wall -Wextra -g
...
//now this loop is not stopping//
...
OUTPUT:-
...
d
100
e
101
此(未定义)行为是写出错误大小 0 的结果,i 在您输入 'd' 后变为 100,然后在您输入 [ 后变为 101 =35=]因为'd'和'e'的ASCII码分别是100和101.
正如我在评论中所说:
i=0;; char a[i];
declare an array of 0 elements. Do scanf("%d",&n);char a[n];
allowing to have its size . First index of an array is 0, not 1, so replace for (i=1;i<=n;i++)
by for (i=0;i<n;i++)
. I also encourage you to check scanf("%d",&n);
returns 1 and n is positive
所以我不明白你为什么回复:
This doesn't work i already tried this
除非你错过了我的话中的至少一个更正,因为我说的都做了:
#include <stdio.h>
int main()
{
int n;
printf("how many characters you want to enter=");
if ((scanf("%d",&n) != 1) || (n < 1))
puts("invalid size");
else {
char a[n];
int i;
for (i=0; i<n; i++)
{
scanf(" %c",&a[i]);
printf("%d\n",i);
}
for (i=0; i<n; i++)
{
printf("%c \t",a[i]);
}
putchar('\n');
}
return 0;
}
编译与执行:
pi@raspberrypi:/tmp $ gcc -Wall cc.c
pi@raspberrypi:/tmp $ ./a.out
how many characters you want to enter=4
a
0
b
1
c
2
d
3
a b c d
pi@raspberrypi:/tmp $
在下面的代码中,循环必须在第 4 个值处结束,因为它是循环的结尾,但取而代之的是,它开始使用甚至不遵循循环条件的值,即 100,甚至不是也停止..当我放一个 printf 语句来测试它然后我发现它在 a[i] 中使用 100 这是不可能的..
#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
int n,i,m;
i=0;
char a[i];
printf("how many characters you want to enter=");
scanf("%d",&n);
for (i=1;i<=n;i++)
{
scanf(" %c",&a[i]);
printf("%d\n",i);
//now this loop is not stopping//
}
for (i=1;i<=n;i++)
{
printf("%c \t",a[i]);
}
}
输出:-
how many characters you want to enter=4
a
1
b
2
c
3
d
100
e
101
i=0;
char a[i];
是undefined behavior so wrong. You should allocate an automatic variable which is a non-empty array, or preferably use carefully C dynamic memory allocation
使用前阅读Modern C, see this C reference, the documentation of your C compiler (e.g. GCC...) and of your debugger (e.g. GDB...). Be sure to read the documentation of every standard function (e.g. of scanf
)。请注意 scanf
可能会失败,并且 returns 是一个您应该测试的有趣整数。
如果您使用 GCC,请编译 with 所有警告和调试信息,因此 gcc -Wall -Wextra -g
...
//now this loop is not stopping//
...
OUTPUT:-
... d 100 e 101
此(未定义)行为是写出错误大小 0 的结果,i 在您输入 'd' 后变为 100,然后在您输入 [ 后变为 101 =35=]因为'd'和'e'的ASCII码分别是100和101.
正如我在评论中所说:
i=0;; char a[i];
declare an array of 0 elements. Doscanf("%d",&n);char a[n];
allowing to have its size . First index of an array is 0, not 1, so replacefor (i=1;i<=n;i++)
byfor (i=0;i<n;i++)
. I also encourage you to checkscanf("%d",&n);
returns 1 and n is positive
所以我不明白你为什么回复:
This doesn't work i already tried this
除非你错过了我的话中的至少一个更正,因为我说的都做了:
#include <stdio.h>
int main()
{
int n;
printf("how many characters you want to enter=");
if ((scanf("%d",&n) != 1) || (n < 1))
puts("invalid size");
else {
char a[n];
int i;
for (i=0; i<n; i++)
{
scanf(" %c",&a[i]);
printf("%d\n",i);
}
for (i=0; i<n; i++)
{
printf("%c \t",a[i]);
}
putchar('\n');
}
return 0;
}
编译与执行:
pi@raspberrypi:/tmp $ gcc -Wall cc.c
pi@raspberrypi:/tmp $ ./a.out
how many characters you want to enter=4
a
0
b
1
c
2
d
3
a b c d
pi@raspberrypi:/tmp $