请问各位:在vc++中怎样定义动态数组。
还有我写了如下的代码,为什么也定义不了普通的数组??
float zhuanzhi(float k[];int weishu)
{
const int nn=weishu
float shuzu[nn][nn];
//我是想定义一个未知数nn维的数组。
}
你可以这么写
int weishu;
cin>>weishu;
const int nn = weishu;
float shuzu[nn][nn];
我已经试过了,你说的方法不可行。下面是报错。
:\Documents and Settings\xxrrdd\dongtaishuzu.cpp(9) : error C2057: expected constant expression
C:\Documents and Settings\xxrrdd\dongtaishuzu.cpp(9) : error C2466: cannot allocate an array of constant size 0
C:\Documents and Settings\xxrrdd\dongtaishuzu.cpp(9) : error C2057: expected constant expression
C:\Documents and Settings\xxrrdd\dongtaishuzu.cpp(9) : error C2466: cannot allocate an array of constant size 0
C:\Documents and Settings\xxrrdd\dongtaishuzu.cpp(9) : error C2087: '<Unknown>' : missing subscript
C:\Documents and Settings\xxrrdd\dongtaishuzu.cpp(9) : error C2133: 'shuzu' : unknown size
Error executing cl.exe.
[此贴子已经被作者于2006-4-11 18:32:00编辑过]
我已经试过了,你说的方法不可行。下面是报错。
:\Documents and Settings\xxrrdd\dongtaishuzu.cpp(9) : error C2057: expected constant expression
C:\Documents and Settings\xxrrdd\dongtaishuzu.cpp(9) : error C2466: cannot allocate an array of constant size 0
C:\Documents and Settings\xxrrdd\dongtaishuzu.cpp(9) : error C2057: expected constant expression
C:\Documents and Settings\xxrrdd\dongtaishuzu.cpp(9) : error C2466: cannot allocate an array of constant size 0
C:\Documents and Settings\xxrrdd\dongtaishuzu.cpp(9) : error C2087: '<Unknown>' : missing subscript
C:\Documents and Settings\xxrrdd\dongtaishuzu.cpp(9) : error C2133: 'shuzu' : unknown size
Error executing cl.exe.
你不要用VC这个东西来编译,用DEV,不信你把下面这个程序在DEV中编译,绝对可以
#include <iostream>
using namespace std;
int main()
{
int weishu;
cin>>weishu;
const int nn = weishu;
float shuzu[nn][nn];
for(int i = 0;i < nn;i++)
{
for(int j = 0;j < nn;j++)
{
shuzu[nn][nn] = i * j;
cout<<shuzu[nn][nn]<<" ";
}
cout<<endl;
}
cout<<endl;
system("pause");
return 0;
}