我要疯了,把一些东西归结为简单的东西,无论我如何尝试或尝试什么,数组都不起作用?怎么了?
代码只是来自 cocos2dx 的单个 cpp helloworld。仅此而已,
double *Array = new double[333];
if (Array == nullptr)
CCLOG("Error: memory could not be allocated");
//initialize it
for( int i = 0; i != 333; ++i){
Array[i] = 333 - i;
}
for( int i = 0; i != 333; ++i){
CCLOG("Hi %ld", Array[i] );
}
循环总是打印 0....
我试了很多循环,测试,数组永远不是数组。它总是只是一个 int、double 或我尝试的任何类型的数组?
有什么想法吗?
VS2012 cocos2dx helloworld 现在只剩下一个数组了。 Windows 10
最佳答案
因为您正在尝试以 %ld
格式显示 double 值。 %ld
只能长期使用。恕我直言,你应该:
将
Array[i]
转换为 long :CCLOG("Hi %ld", (long) Array[i] );
或者使用
%g
格式:CCLOG("Hi %g", Array[i] );
这两种方法都应该给你正确的显示。
关于c++ - "new"无法创建数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27764531/