#include<stdio.h>
void main()
{
Item_base **pp = new Item_base[10];
for(int i = 0; i < 10; ++i)
{
pp[i] = new Item_base();
}
char **p;
char **n;
p = (char**)malloc(10 * sizeof(char*)); //分配第二维空间
n = p;
for(int i = 0; i < 10; ++i) //分配第一维空间
{
p[i] = (char*)malloc(50 * sizeof(char));
}
for(int i = 0; i < 10; ++i)
{
/*p[i] = "test";*/
//*n++ = "test";
strcpy(p[i], "test"); //如果没分配第一维空间,则次语句执行失败
}
int i = 0;
for(; i < 10; ++i)
{
printf("%s/t", p[i]);
}
}
//动态分配指向对象的指针数组
#include <iostream>
class OBJ {
public:
OBJ() {
std::cout << "Constructor" << std::endl;
}
~OBJ() {
std::cout << "Destructor" << std::endl;
}
};
int main(int argc, char *argv[]) {
OBJ **a;
a = new OBJ*[10]; //分配二维的数组空间
for (int i = 0; i < 10; ++i) {
a[i] = new OBJ(); // 定义一维空间中的对象
}
for (int i = 0; i< 10; ++i) {
delete a[i]; //删除一维的空间中的对象
}
delete [] a; //释放二维的数组空间
return EXIT_SUCCESS;
}
void main()
{
Item_base **pp = new Item_base[10];
for(int i = 0; i < 10; ++i)
{
pp[i] = new Item_base();
}
char **p;
char **n;
p = (char**)malloc(10 * sizeof(char*)); //分配第二维空间
n = p;
for(int i = 0; i < 10; ++i) //分配第一维空间
{
p[i] = (char*)malloc(50 * sizeof(char));
}
for(int i = 0; i < 10; ++i)
{
/*p[i] = "test";*/
//*n++ = "test";
strcpy(p[i], "test"); //如果没分配第一维空间,则次语句执行失败
}
int i = 0;
for(; i < 10; ++i)
{
printf("%s/t", p[i]);
}
}
//动态分配指向对象的指针数组
#include <iostream>
class OBJ {
public:
OBJ() {
std::cout << "Constructor" << std::endl;
}
~OBJ() {
std::cout << "Destructor" << std::endl;
}
};
int main(int argc, char *argv[]) {
OBJ **a;
a = new OBJ*[10]; //分配二维的数组空间
for (int i = 0; i < 10; ++i) {
a[i] = new OBJ(); // 定义一维空间中的对象
}
for (int i = 0; i< 10; ++i) {
delete a[i]; //删除一维的空间中的对象
}
delete [] a; //释放二维的数组空间
return EXIT_SUCCESS;
}