C ++代码。谁能帮助我了解为什么这不起作用?

时间:2020-09-01 03:55:46

标签: c++ arrays

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{


    const int SIZES = 4;
    int oldvalues[SIZES] = { 10, 100, 2000, 300 };
    int newvalues[SIZES];



    for (int count = 0; count < SIZES; count++)
        newvalues[count] = oldvalues[count];
    cout << newvalues << endl;

}

我的代码仅打印“ 0x7ffeefbff270”是有原因的吗,我认为没有任何遗漏。我的猜测是我的提示错了吗?

2 个答案:

答案 0 :(得分:0)

打印出数组的值时,必须指定要打印的每个元素。您无法一次打印整个内容。

您应该在for循环中放置方括号(始终使用方括号!),并将提示行包括在这些方括号中。然后更新提示行以引用您刚刚保存的元素-newvalues [count]。

答案 1 :(得分:0)

仅尝试打印阵列即可为您提供一个内存地址。

#include <iostream>

int main()
{
    const int SIZES = 4;
    int oldvalues[SIZES] = { 10, 100, 2000, 300 };
    int newvalues[SIZES];


    for (int count = 0; count < SIZES; count++)
        newvalues[count] = oldvalues[count];

    for (int count = 0; count < SIZES; count++)
        std::cout << newvalues[count] << " "; # Here you need to indicate each element
}

相关问题