gpt4 book ai didi

C++分配数组语法问题

转载 作者:太空宇宙 更新时间:2023-11-03 10:35:30 26 4
gpt4 key购买 nike

你可以这样写:

int test[] = {1,2,3,4};

但是如果你想使用一个指针并用new分配它呢?

int *test2;
test2 = new int[]{1,2,3,4};

这行不通,语法是什么或者可能吗?

最佳答案

这是当前标准 C++ 的缺点之一。初始化不统一。你最大的希望is uniform initialization in C++0x ,但同时您可以在创建数组后分配值,例如

int *test2;
test2 = new int[4]; // Note that you still have to specify the size in C++0x
test2[0] = 1;
test2[1] = 2;
test2[2] = 3;
test2[3] = 4;

关于C++分配数组语法问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4646717/

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