gpt4 book ai didi

c++ - "error: taking address of temporary array"是什么意思?

转载 作者:行者123 更新时间:2023-11-30 00:44:49 30 4
gpt4 key购买 nike

当我尝试运行以下命令时

// Example program
#include <iostream>
#include <string>

int* x;

int main()
{
x = (int[5]) { 16, 2, 77, 40, 12071 };

std::cout << x;
}

我收到以下消息 error: taking address of temporary array

这是什么意思?

最佳答案

(int[5]) { 16, 2, 77, 40, 12071 } 是一个匿名临时。一旦分配完成,它就会超出范围。

这给您留下了一个悬挂指针x 在全局命名空间中没有区别。

改用std::vector;利用初始化列表构造。

关于c++ - "error: taking address of temporary array"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46521343/

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