程序
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
int main(int argc, char** argv)
{
if(argc < 2)
exit(1);
cout<<strtof(argv[1],NULL);
int SIZE = (int) (strtof(argv[1],NULL)/8);
int **arr = new int[SIZE][SIZE*4]();
for(int i = 0; i < SIZE; i++) {
for(int j = 0; j < SIZE*4; j++) {
cout<<arr[i][j];
}
}
return 0;
}输入
32
错误
prog.cpp: In function ‘int main(int, char**)’: prog.cpp:13:39: error: array size in new-expression must be constant
int **arr = new int[SIZE][SIZE*4]();
^
prog.cpp:13:39: error: the value of ‘SIZE’ is not usable in a constant expression prog.cpp:12:9: note: ‘int SIZE’ is not const
int SIZE = (int) (strtof(argv[1],NULL)/8);
^~~~在C++11中是否有其他方法,而不是使用malloc或calloc来实现这一点?
Ideone代码
发布于 2019-09-09 13:50:10
如果您真的不想使用vector,那么这个工作代码可能会有所帮助。
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
int main(int argc, char** argv)
{
if(argc < 2)
exit(1);
cout<<strtof(argv[1],NULL);
int SIZE = (int) (strtof(argv[1],NULL)/8);
int** arr = new int*[SIZE];
for(int i = 0; i < SIZE; i++) {
arr[i] = new int[ 4* SIZE];
}
for(int i = 0; i < SIZE; i++) {
for(int j = 0; j < SIZE*4; j++) {
cout<<arr[i][j];
}
}
return 0;
}https://stackoverflow.com/questions/57853703
复制相似问题