gpt4 book ai didi

c++ - 为什么我的代码打印地址而不是数组内容?

转载 作者:行者123 更新时间:2023-11-28 00:13:58 26 4
gpt4 key购买 nike

我是 C++ 的新手,需要这方面的帮助。我想打印“C”的值,但它一直在打印它的地址。这是我的代码:

#include <iostream>
using namespace std;



int* test (int a[5], int b[5]) {
int *c = new int[5];
for (int i = 0; i < 5; i++) c[i] = a[i]+b[i];
return c;
}


int main() {
int A[5] = {22, 33, 44, 55, 66};
int B[5] = {22, 33, 44, 55, 66};
int* C[10] = {test(A,B)};

for (int i = 0; i < 10; i++) cout<< &C[i] <<endl;

return 0;
}

提前致谢。

最佳答案

第一个int* C[10] = {test(A,B)};应该是 int * C = test(A,B);当您在函数中创建数组并返回指向它的指针时。其次你应该调用delete [] C在您调用的 main 末尾 new[]每次你打电话new[]你应该调用delete[] .最后,您在 cout<< &C[i] <<endl; 中使用运算符 (&) 的地址正如它的名字所说,它会给你地址 C[i]而不是它的值(value)。

关于c++ - 为什么我的代码打印地址而不是数组内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31524402/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com