C++结构体指针数组如何分配空间,用new

typedef struct tagMyLine{
POINT S;
POINT E;
LOGPEN LinePen;
}MYLINE,*PMYLINE;

PMYLINE pMyline[MAX_LINE];

2.指针数组:一个数组里存放的都是同一个类型的指针,通常我们把他叫做指针数组。
比如 int * a[10];它里边放了10个int * 型变量,由于它是一个数组,已经在栈区分配了10个(int * )的空间,也就是32位机上是40个byte,每个空间都可以存放一个int型变量的地址,这个时候你可以为这个数组的每一个元素初始化,在,或者单独做个循环去初始化它。
3.例子:
int * a[2]={ new int(3),new int(4) }; //在栈区里声明一个int * 数组,它的每一个元素都在堆区里申请了一个无名变量,并初始化他们为3和4,注意此种声明方式具有缺陷,VC下会报错
例如 :
int * a[2]={new int[3],new int[3]};
delete a[0];
delet a[10];

int * a[2];
a[0]= new int[3];
a[1]=new int[3];
delete a[0];
delet a[10];
这样申请内存的风格感觉比较符合大家的习惯;由于是数组,所以就不可以delete a;编译会出警告.delete a[1];
注意这里 是一个数组,不能delete [] ;

4.指针的指针;
int ** cc=new (int*)[10]; 声明一个10个元素的数组,数组每个元素都是一个int *指针,每个元素还可以单独申请空间,因为cc的类型是int*型的指针,所以你要在堆里申请的话就要用int *来申请;
看下边的例子 (vc & GNU编译器都已经通过);
int ** a= new int * [2];     //申请两个int * 型的空间
a[1]=new int[3];        //为a的第二个元素又申请了3个int 型空间,a[1]指向了此空间首地址处
a[0]=new int[4];        ////为a的第一个元素又申请了4个int 型空间,a[0] 指向了此空间的首地址处
int * b;
a[0][0]=0;
a[0][1]=1;
b=a[0];
delete [] a[0]       //一定要先释放a[0],a[1]的空间,否则会造成内存泄露.;
delete [] a[1];
delete [] a;
b++;
cout<<*b<<endl; //随机数
5.注意 :因为a 是在堆里申请的无名变量数组,所以在delete 的时候要用delete [] 来释放内存,但是a的每一个元素又单独申请了空间,所以在delete [] a之前要先delete [] 掉 a[0],a[1],否则又会造成内存泄露.
#include <iostream>
#include <vector>
#include <map>
#include <iterator>
#include <algorithm>

using namespace std;

typedef class student
{
public:
int number;
char* name;
}STU, *LPSTU;

vector<LPSTU>vec;
vector<LPSTU>::iterator iter;

map<int,LPSTU>mapstu;
map<int,LPSTU>::iterator itermap;

void main(void)
{
char* str1 = "YaoMing";
char* str2 = "T_MAC";
char* str3 = "Join";

LPSTU stu[3];

for (int n=0; n < 3; n++)
{
stu[n] = new student;
}

stu[0]->name = new char[strlen(str1)+1];
strcpy(stu[0]->name,str1);
stu[0]->number = 1;

stu[1]->name = new char[strlen(str2)+1];
strcpy(stu[1]->name,str2);
stu[1]->number = 2;

stu[2]->name = new char[strlen(str3)+1];
strcpy(stu[2]->name,str3);
stu[2]->number = 3;

int i = 0;
for(;i < 3 ; i++)
{
vec.push_back(stu[i]);
mapstu.insert(make_pair(stu[i]->number,stu[i]));
}
for (iter=vec.begin();iter!=vec.end();++iter)
{
cout<< (*iter)->name<<"/t"<< (*iter)->number<<endl;
}

for (itermap = mapstu.begin();itermap != mapstu.end(); ++itermap)
{
cout<<itermap->first<<"/t"<<itermap->second->name<<endl;
}

for( i = 0;i < 3 ; i++)
{
vec.pop_back();
mapstu.erase(stu[i]->number);
}
cout<<vec.size()<<"/t"<<mapstu.size()<<endl;

for( i = 0;i < 3 ; i++)
{
delete[] stu[i]->name;
delete stu[i];
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2016-01-08
比如一个包含3个元素的CandyBar数组,使用new来动态分配数组,并初始化,然后显示,代码如下:
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
using namespace std;
struct CandyBar{
CandyBar():brand(""),weight(0.0),calorie(0){
printf("brand:%s,weight:%lf,calorie:%d\n",brand.c_str(),weight,calorie);
}
string brand;
double weight;
int calorie;
};
int main()
{
struct CandyBar* pCandyBar = new struct CandyBar[3];
}
第2个回答  推荐于2018-03-11
PMYLINE pMyline = new MYLINE[MAX_LINE];追问

但是我定义的是指针数组啊

追答

上述语句定义的是PMYLINE类型的数组,该类型的指针型数组貌似可以这样定义:
PMYLINE *pMyline = new *MYLINE[MAX_LINE];

你再试试,因为我没有编译验证。

追问

如果要操作指针数组,是不是*pMyline+sizeof(MYLINE)
MYLINEi是结构体类型

追答

不对,应该和操作基础类型事故组一样,该类型变量占用内存的字节数有编译程序完成,就是说,可以这样操作。
MYLINE MyLine[i] = pMyLine[j][k];

追问

我的意思是不用结构体数组变量,用结构体指针数组。。也就是**P,该如何操作

追答

上面说了,操作方法与基础类型完全一样,如果基础类型二级指针的操作比较熟练的话,搬到构造数据类型的操作上即可,但假定不是很熟练的话,建议你用数组方式,调试通过后,再改写为二级指针操作,我以为,这是较为稳妥的、可靠的学习路子,你说呢?

本回答被提问者和网友采纳
第3个回答  2012-11-18
PMYLINE pMyline = new PMYLINE[MAX_LINE];
第4个回答  2012-11-18
PMYLINE pMyline[MAX_LINE] = new pMyline[MAX_LINE]追问

error C2061: syntax error : identifier 'pMyline'

追答

PMYLINE *pMyline= new pMyline[MAX_LINE] ;
用指针

追问

syntax error : identifier 'pMyline'

追答

PMYLINE *pMyline= new PMYLINE [MAX_LINE] ;
写错了,把变量名做结构体名了,不好意思

追问

试过了,还是出现冲突访问错误,如果直接用结构体,没问题。一用指针就出错,
程序中需要使用的是数组指针,那么想要访问第二个结构体该怎么操作?需要的是pMyline[]

相似回答