它是否正确 ?
这是用 g++ (3.4) 成功编译的。
int main()
{
int x = 12;
char pz[x];
}
这是所有其他问题的组合答案:
你现在的代码是not标准C++。它is标准C99。这是因为 C99 允许您以这种方式动态声明数组。澄清一下,这也是标准 C99:
#include <stdio.h>
int main()
{
int x = 0;
scanf("%d", &x);
char pz[x];
}
This is not标准的任何东西:
#include <iostream>
int main()
{
int x = 0;
std::cin >> x;
char pz[x];
}
它不能是标准 C++,因为它需要恒定的数组大小,并且它不能是标准 C,因为 C 没有std::cin
(或者命名空间,或者类,等等......)
要使其成为标准 C++,请执行以下操作:
int main()
{
const int x = 12; // x is 12 now and forever...
char pz[x]; // ...therefore it can be used here
}
如果你想要一个动态数组,你can做这个:
#include <iostream>
int main()
{
int x = 0;
std::cin >> x;
char *pz = new char[x];
delete [] pz;
}
但你应该这样做:
#include <iostream>
#include <vector>
int main()
{
int x = 0;
std::cin >> x;
std::vector<char> pz(x);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)