c初学者,向量
我是c初学者,我有一个问题(和往常一样)。我写了一个简单的程序:c初学者,向量,c,vector,C,Vector,我是c初学者,我有一个问题(和往常一样)。我写了一个简单的程序: #include <stdio.h> #define SIZE 10 main() { int vettore[9]; int contatore1,contatore2; for(contatore1 = 0; contatore1 <= 9; ++contatore1) { vettore[contatore1] = contatore1*2;
#include <stdio.h>
#define SIZE 10
main()
{
int vettore[9];
int contatore1,contatore2;
for(contatore1 = 0; contatore1 <= 9; ++contatore1)
{
vettore[contatore1] = contatore1*2;
}
printf("%d\n\n", vettore[9]);
for(contatore2 = 0; contatore2 < 10; ++contatore2)
{
printf("%d\n", vettore[contatore2]);
}
printf("\n%d\n", vettore[9]);
return 0;
}
为什么vettore[9]的值会更改3次?为什么它只在输出的第一行有正确的值?谢谢:)C数组是基于零的,因此9元素数组的有效索引是[0..8]。您正在写入超出数组末尾的内容。这有未定义的结果,但可能会损坏下一个堆栈变量 更详细地说
vettore
有9个元素,可以使用vettore[0]
访问这些元素<代码>vettore[8]。第一个循环的最后一次迭代写入vettore[9]
。这将访问数组末尾以外的内存。这会导致未定义的行为(即C标准未在此指定预期结果),但vettore[9]
的地址可能与contatore2
的地址相同,这意味着后一个变量被写入
在下一个循环中,您会遇到一个类似的问题,即打印的元素比vettore
包含的元素多
您可以通过将循环更改为
for(contatore1 = 0; contatore1 < 9; ++contatore1)
for(contatore2 = 0; contatore2 < 9; ++contatore2)
for(contatore1=0;contatore1<9;++contatore1)
对于(contatore2=0;contatore2<9;++contatore2)
请注意,如果改为计算数组的大小,则会更安全,方法是在循环的退出测试中使用
sizeof(vettore)/sizeof(vettore[0])
,而不是硬编码9
C数组是基于零的,因此9元素数组的有效索引是[0..8]。您正在写入超出数组末尾的内容。这有未定义的结果,但可能会损坏下一个堆栈变量
更详细地说vettore
有9个元素,可以使用vettore[0]
访问这些元素<代码>vettore[8]。第一个循环的最后一次迭代写入vettore[9]
。这将访问数组末尾以外的内存。这会导致未定义的行为(即C标准未在此指定预期结果),但vettore[9]
的地址可能与contatore2
的地址相同,这意味着后一个变量被写入
在下一个循环中,您会遇到一个类似的问题,即打印的元素比vettore
包含的元素多
您可以通过将循环更改为
for(contatore1 = 0; contatore1 < 9; ++contatore1)
for(contatore2 = 0; contatore2 < 9; ++contatore2)
for(contatore1=0;contatore1<9;++contatore1)
对于(contatore2=0;contatore2<9;++contatore2)
请注意,如果改为计算数组的大小,则会更安全,方法是在循环的退出测试中使用
sizeof(vettore)/sizeof(vettore[0])
,而不是硬编码9
您定义的vettore
大小是9
int vettore[9];
在循环中,您从0
开始,一直到9
,因此您正在使用数组的10
元素,而不是9
(数组大小)
您应该使用大小10
int vettore[10];
您定义的
vettore
大小为9
int vettore[9];
在循环中,您从0
开始,一直到9
,因此您正在使用数组的10
元素,而不是9
(数组大小)
您应该使用大小10
int vettore[10];
您的数组
vettore
有9个元素,但通过引用vettore[9]
,您实际上是在引用第10个元素(因为元素索引从0开始)。所以它是堆栈上的某个随机位置,没有定义好的值
解决方案是只对
vettore[8]
进行索引,或者将vettore
定义为大小为10。您的数组vettore
有9个元素,但通过引用vettore[9]
,实际上是在引用第10个元素(因为元素索引从0开始)。所以它是堆栈上的某个随机位置,没有定义好的值
解决方案是只为vettore[8]
编制索引,或将vettore
定义为大小为10。查看以下内容:
for(contatore2 = 0; contatore2 < 10; ++contatore2)
{
printf("%d\n", vettore[contatore2]);
}
for(contatore2=0;contatore2<10;++contatore2)
{
printf(“%d\n”,vettore[contatore2]);
}
您正在显示vettore数组(定义为9整数数组)的11个元素。我认为错误在于堆栈上的随机分配请检查以下内容:
for(contatore2 = 0; contatore2 < 10; ++contatore2)
{
printf("%d\n", vettore[contatore2]);
}
for(contatore2=0;contatore2<10;++contatore2)
{
printf(“%d\n”,vettore[contatore2]);
}
您正在显示vettore数组(定义为9整数数组)的11个元素。我认为错误在于堆栈上的随机分配数组(即“向量”)从索引0开始,而不是从1开始;例如,它的内容可能是5,但它将占用索引位置0,1,2,3,4
[1][2][3][4][5] <- Five items
0 1 2 3 4 <- Their respective locations in the array
[1][2][3][4][5]数组(即“向量”)从索引0开始,而不是从索引1开始;例如,它的内容可能是5,但它将占用索引位置0,1,2,3,4
[1][2][3][4][5] <- Five items
0 1 2 3 4 <- Their respective locations in the array
[1][2][3][4][5]顺便说一句,你应该遵循1风格-我建议使用“数组”这个名称而不是向量。严格来说,向量是一个正确的术语,但是在C编程中,更多的是指数学向量或C++类型的STD::矢量。没有火焰,只是为了你自己的利益。C语言中正确的术语是“数组”。您应该使用这个术语来避免混淆。顺便说一句,您应该遵循1样式-我建议使用“array”这个名称而不是vector。严格来说,向量是一个正确的术语,但是在C编程中,更多的是指数学向量或C++类型的STD::矢量。没有火焰,只是为了你自己的利益。C语言中正确的术语是“数组”。我刚才问了一个愚蠢的问题。我应该读书