c++代码输出说明
C++ Code Output explanation
我有以下代码,问题要求我找到输出。我通过输入找到了输出(2),但我很难弄清楚如何/为什么。任何帮助吗?
代码如下:
int scores[5];
int *numbers = scores;
for (int i=0; i <=4; i++)
*(numbers+i)=i;
cout << numbers[2] <<endl;
你的代码实际上做了
scores[2] = 2;
cout<<scores[2]<<endl;
因此答案是…
详细说明:
int scores[5];
int *numbers = scores; //numbers points to the memory location of the array scores
for (int i=0; i <=4; i++) // as mentioned, stray ';'
*(numbers+i)=i; //same as numbers[i] = i which is same as scores[i] = i
cout << numbers[2] <<endl;
for循环执行的唯一语句是
*(numbers+i)=i;
将使用差值操作符(*)存储int元素在该位置的索引。
然后打印出第三个数字,它等于2,因为数组从索引0开始。
设置指向数组的第一个内存位置的指针,然后遍历一系列内存地址并对其进行写操作。需要注意的是,使用带有解引用的指针算术
*(pointer + i) = i;
与使用下标操作符相同:
pointer[i] = i;
相关文章:
- 递归函数计算序列中的平方和(并输出过程)
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 请解释"函数1(p1,p2,p3);"的输出
- C++:将控制台输出存储在宏中更好吗
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 为什么我的代码在输出中增加了93天
- 如何从void函数输出字符串
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- AES加密到解密未正确输出
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 当使用通配符和null指针调用函数时,对输出的说明
- 有没有办法我可以在 C++ 中使用 if 语句来说明如果一个字符串不是两个名称的 eqal,它会给出一个输出,ATM i
- C++ "#include"输出说明
- 使用向量(STL)时,未定义的行为,请说明理性背后的以下代码输出
- 宏输出说明
- 一个数字和字段宽度的格式化输出,C++标准在哪里说明它
- c++代码输出说明