如何定义一个动态的二维数组

micosun6 2008-09-26 06:01:41
char rlname[100][30];

要动态的
...全文
233 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
wzhwisk 2011-11-20
  • 打赏
  • 举报
回复
链表+malloc函数是王道
ismarterworld 2011-10-31
  • 打赏
  • 举报
回复
[Quote=引用楼主 micosun6 的回复:]
char rlname[100][30];

要动态的
[/Quote]
for(i=0; i<column; i++)
rlname[i] = (char *)malloc(sizeof(char) * column);
应该为:
for(i=0; i<row; i++)
rlname[i] = (char *)malloc(sizeof(char) * column);
sd01101230 2008-09-27
  • 打赏
  • 举报
回复
用STL
lyghe 2008-09-27
  • 打赏
  • 举报
回复
动态数组。。。。。。。。。谭浩强在十几年前就告诉我们,那是不可能地。

用vector吧。

可怜的数组。。。。。。。
屋顶上的老猫 2008-09-27
  • 打赏
  • 举报
回复
跟数组较什么劲,用链表才是解决动态分配的王道
fallening 2008-09-27
  • 打赏
  • 举报
回复
不过阿,现在编译器支持这样的声明了:
int M;
int N;
cin >> M >> N;
int arr[M][N];
richbirdandy 2008-09-27
  • 打赏
  • 举报
回复

using namespace boost::numeric::ublas;
matrix<char> rlname(100,30);
richbirdandy 2008-09-27
  • 打赏
  • 举报
回复

using namespace boost::numeric::ublas;
matrix<long> re(2,2);
re(0,0)=1;re(0,1)=0;re(1,0)=0;re(1,1)=1;//一个单位矩阵
Kenny_Glacier 2008-09-26
  • 打赏
  • 举报
回复
char* a = malloc(sizeof(char)*r*c);
*(a+c*j+i)
devil_zuiai 2008-09-26
  • 打赏
  • 举报
回复
char ** Array;
int nRows = 0;
int nCols = 0;
cout << "Input rows:";
cin >> nRows;
cout << "Input cols:";
cin >> nCols;
Array = new char*[nRows];
for (int i = 0; i < nRows; i++)
{
Array[i] = new char[nCols];
}

其实对于2维数组你可以进行降维处理,
即直接
char * Array = new char[nRows*nCols];
taojian_hhu 2008-09-26
  • 打赏
  • 举报
回复
int m,n,**p;
cin>>m>>n;
p=new int*[m];
for(int i=0;i<m;++i)//for allocate memory
{
*p+i=new int[n];
}
....
Process your program;
for(int j=0;j<m;++j)//for free the heap memory
{
delete []p[j];
}
delete []p;
kingstarer 2008-09-26
  • 打赏
  • 举报
回复
上面的说明写错了

首先,声明N个指针,每个指针各指向一个包含M个元素的数组
其次,声明一个指针数组的指针,指向一个含有N个元素的指针数组,将N个指针存到该指针数组中
这样就成了一个动态的二维数组了
kingstarer 2008-09-26
  • 打赏
  • 举报
回复
动态二维数组可以这样实现

首先,声明M个指针,每个指针各指向一个数组
其次,声明一个指针数组的指针,指向一个含有N个元素的指针数组,将N个指针存到该指针数组中
这样就成了一个动态的二维数组了


#define N 10
#define M 50
int *pLine;//指向数组的指针
int **ppCow;//指向指针数组的指针

//申请
ppCow = new int *[N];
for(int i = 0; i < N; i++)
{
pLine = new int[M];
ppCow[i] = pLine;
}

//释放
for(int i = 0; i < N; i++)
{
delete []ppCow[i];
}
#undef M
#undef N

就呆在云上 2008-09-26
  • 打赏
  • 举报
回复
//like this!

#include <stdio.h>
#include <malloc.h>

int main() {
char **rlname;

//malloc!
rlname = (char**)malloc(100 * sizeof(char*) );
for (int j = 0; j < 100; j++)
{
rlname[j] = (char*) malloc(30 * sizeof(char) );
}
//free

for (int j = 0; j < 100; j++)
{
free(rlname[j]);
}
free(rlname);
}
bargio_susie 2008-09-26
  • 打赏
  • 举报
回复
       int row, column;
int i;
char ** rlname;

printf("Please input row:");
scanf("%d", &row);
rlname = (char **)malloc(sizeof(char *) * row);

printf("Please input column:");
scanf("%d", &column);

for(i=0; i<column; i++)
rlname[i] = (char *)malloc(sizeof(char) * column);
bargio_susie 2008-09-26
  • 打赏
  • 举报
回复
       int row, column;
int i;
char ** rlname;

printf("Please input row:");
scanf("%d", &row);
rlname = (char **)malloc(sizeof(char *) * row);

printf("Please input column:");
scanf("%d", &column);

for(i=0; i<column; i++)
rlname[i] = (char *)malloc(sizeof(char) * column);
kkndciapp 2008-09-26
  • 打赏
  • 举报
回复
void main()
{
char **p=new char*[100];//先在一维上分配
for(int i=0;i<100;i++)
p[i]=new char[30];//再在二维上分配
//////////////////
for(int j=0;j<100;j++)
delete p[j];//先在二维上释放
delete p;//再释放一维的
}
xianyuxiaoqiang 2008-09-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 bargio_susie 的回复:]
C/C++ code int row, column;
int i;
char ** rlname;

printf("Please input row:");
scanf("%d", &row);
rlname = (char **)malloc(sizeof(char *) * row);

printf("Please input column:");
scanf("%d", &column);

for(i=0; i<column; i++)
rlname[i] = (char *)malloc(sizeof(char) * column);
[/Quote]
up
user_csc 2008-09-26
  • 打赏
  • 举报
回复
char array[M][N];

char *array= malloc(sizeof(char) * M * N )

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧