具有指针增量假值的C程序
我想知道是否有人能帮我做下面的C程序。代码只是创建一个指针,然后为指针分配一个整数数组的地址。从那里,指针被赋予一个值,然后递增。 输出显示数组名称、数组地址和数组值。输出按预期工作,但数组中的最后一项增加了5,而不是1 代码如下所示:具有指针增量假值的C程序,c,arrays,pointers,C,Arrays,Pointers,我想知道是否有人能帮我做下面的C程序。代码只是创建一个指针,然后为指针分配一个整数数组的地址。从那里,指针被赋予一个值,然后递增。 输出显示数组名称、数组地址和数组值。输出按预期工作,但数组中的最后一项增加了5,而不是1 代码如下所示: #include <stdio.h> main() { int numar[4]; //integer array int x; //value for loop int *p;
#include <stdio.h>
main()
{
int numar[4]; //integer array
int x; //value for loop
int *p; //pointer
p=numar; //'point' p to address of numar array (no & necessary as arrays are pointers)
for(x=0;x<5;x++) //loop 0-4
{
*p=x; //assign the value of the first item in array to x
p++; //increment pointer for next iteration
}
for(x=0;x<5;x++) //loop 0-4
{
//display output of array name, array location and array value
printf("array[%d] is at address %p with value %d\n", x,&numar[x],numar[x]);
}
return 0;
}
array[0] is at address 0061FF18 with value 0
array[1] is at address 0061FF1C with value 1
array[2] is at address 0061FF20 with value 2
array[3] is at address 0061FF24 with value 3
array[4] is at address 0061FF28 with value 8
正如您所看到的,数组[4]所需的值应该是4,但实际上是8
任何帮助都将不胜感激
非常感谢,为了使您的代码能够编译,我必须做一个更改:我在“main”之前添加了“int”,以便main函数能够正确返回0 问题是您创建了一个大小为4的整数数组,但希望它包含5项
int numar[4];
表示数组是为4个元素定义的:
numar[0]
numar[1]
numar[2]
numar[3]
所以,
numar[4]
没有定义
要解决此问题,请将阵列增大一个大小:
int numar[5];
我得到的结果是:
array[0] is at address 0x7fff557f0730 with value 0
array[1] is at address 0x7fff557f0734 with value 1
array[2] is at address 0x7fff557f0738 with value 2
array[3] is at address 0x7fff557f073c with value 3
array[4] is at address 0x7fff557f0740 with value 4
为了使您的代码能够编译,我必须做一个更改:我在“main”之前添加了“int”,以便main函数能够正确返回0 问题是您创建了一个大小为4的整数数组,但希望它包含5项
int numar[4];
表示数组是为4个元素定义的:
numar[0]
numar[1]
numar[2]
numar[3]
所以,
numar[4]
没有定义
要解决此问题,请将阵列增大一个大小:
int numar[5];
我得到的结果是:
array[0] is at address 0x7fff557f0730 with value 0
array[1] is at address 0x7fff557f0734 with value 1
array[2] is at address 0x7fff557f0738 with value 2
array[3] is at address 0x7fff557f073c with value 3
array[4] is at address 0x7fff557f0740 with value 4
用于数组的索引有问题 您正在使用
int numar[4]
创建一个4元素数组。这将创建具有有效索引0、1、2和3的数组这里没有索引[4]
强>
因此,当您以后在for
循环中填充和访问数组时,应如下设置:
for(x = 0; x < 4; x++) {
(x=0;x<4;x++)的{
x<**4**
,而不是5
。此循环将检查索引0、1、2和3
循环访问不属于数组的元素
[4]
。数组使用的索引有问题
您正在使用int numar[4]
创建一个4元素数组。这将创建具有有效索引0、1、2和3的数组。此处没有索引[4]
!
因此,当您以后在for
循环中填充和访问数组时,应如下设置:
for(x = 0; x < 4; x++) {
(x=0;x<4;x++)的{
x<**4**
,而不是5
。此循环将检查索引0、1、2和3
您的循环访问不属于数组的元素
[4]
。很抱歉,无法使数组的索引从1开始,因此它们从0开始。因此,如果您有一个包含4个元素的数组,则最后一个元素是索引为3而不是4的元素
在代码中,您正在读取5个元素,其中只有4个元素的数组
for(x = 0; x < 5; x++) // iterating five times not 4: 0, 1, 2, 3, 4
//....
(x=0;x<5;x++)的for//迭代五次而不是4:0、1、2、3、4
//....
*写入数组的非元素最糟糕的事情是加密其他变量,例如:
int a = 0; // initialized to avoid compiler complaining later on
int numar[4] = {0, 1, 2, 3}; // ok
numar[4] = 77; // writing to the fifth element which doesn't belong to numar. as you can see variable a right before numar
cout << a << endl; // a: 77!! where did a get this value? because a is right before numar so element five which doesn't belong to numar was allocated to a
int a=0;//初始化以避免以后编译器抱怨
int numar[4]={0,1,2,3};//好的
numar[4]=77;//写入不属于numar的第五个元素。正如您在numar前面看到的变量a一样
cout很抱歉,无法使数组的索引从1开始,因此它们从0开始。因此,如果数组包含4个元素,则最后一个元素是索引为3而不是4的元素
在代码中,您正在读取5个元素,其中只有4个元素的数组
for(x = 0; x < 5; x++) // iterating five times not 4: 0, 1, 2, 3, 4
//....
(x=0;x<5;x++)的for//迭代五次而不是4:0、1、2、3、4
//....
*写入数组的非元素最糟糕的事情是加密其他变量,例如:
int a = 0; // initialized to avoid compiler complaining later on
int numar[4] = {0, 1, 2, 3}; // ok
numar[4] = 77; // writing to the fifth element which doesn't belong to numar. as you can see variable a right before numar
cout << a << endl; // a: 77!! where did a get this value? because a is right before numar so element five which doesn't belong to numar was allocated to a
int a=0;//初始化以避免以后编译器抱怨
int numar[4]={0,1,2,3};//好的
numar[4]=77;//写入不属于numar的第五个元素。正如您在numar前面看到的变量a一样
coutint numar[4];
但是看起来您访问了索引4(最大有效值为3),因此您运行了数组的末尾并调用了未定义的行为。下面是鼻魔….[4]
意味着数组有4个元素数组不是指针。计算为数组“decays”的(子)表达式指向该数组的第一个元素的指针(因此您的赋值是有效的),但这完全是另一回事。愚蠢的错误。谢谢。int-numar[4];
但是看起来您访问了索引4(max-valid是3),因此您运行数组的末尾并调用未定义的行为。鼻腔恶魔来了。[4]
表示数组有4个元素数组不是指针。计算为数组的(子)表达式“衰减”为指向该数组第一个元素的指针(因此赋值有效),但那完全是另一回事。愚蠢的错误。谢谢。非常感谢你的回答。代表我犯了一个愚蠢的错误。非常感谢你的回答。代表我犯了一个愚蠢的错误。