动态创建二维数组

来源:互联网 发布:ssd caffe 源码解读 编辑:程序博客网 时间:2024/06/15 22:27

利用数组指针

char(*c)[5];try{c = new char[n][5];}catch(bad_alloc){    cerr<<"Out of Memory"<<endl;    exit(1);}

其中c 是一个数组指针,[5]指定的是列数。

利用指针的指针

try{    char **x = new char *[numberOfRows];    for(int i=0;i<numberOfRows;i++)        x[i] = new char[numberOfColumns];    return true;}catch(bad_alloc){      return false;}//delete//删除行数组空间for(int i=0;i<numberOfRows;i++)    delete [] x[i];//删除行指针delete []x;x=NULL;
原创粉丝点击