请问如何在c语言中实现动态的申请一个数组?

alexzyf 2003-11-10 08:42:41
希望在c(不是c++)中实现动态的定义一个数组的大小,例如 int tab[n][m],数组的大小有n和m的大小所控制,或则在定义了一个数组的大小以后,可以在使用之前从新定义该数组的大小。

多谢了!!
...全文
237 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
laomai 2003-11-16
  • 打赏
  • 举报
回复
经过调试后的例子,朋友们帮忙看看还有什么问题。
typedef int DataType;
/*建立动态二维数组*/
DataType** CreateArray(DataType** p,int row,int column)
{
p=(DataType**)malloc(sizeof(DataType*)*row);
for(int i=0;i<row;i++)
{
p[i] = (DataType*)malloc(sizeof(DataType)*column);
}
return p;
}
/*处理二维数组 */
DataType** SetArray(DataType**p,int row,int column)
{
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
p[i][j]=i*column+j;
}
return p;
}
void DisplayArray(DataType**p,int row,int column)
{
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
printf("%d",p[i][j]);
}
printf("\n");
}
}
/*释放二维数组*/
void FreeArray(DataType** p,int row)
{
for(int i=0;i<row;i++)
{
free(p[i]);
}
free(p);
}

main()
{
DataType** p; //数组的头指针
int row,column; //行数与列数
scanf("%d",&row);
scanf("%d",&column);
p=CreateArray(p,row,column);
p=SetArray(p,row,column);
DisplayArray(p,row,column);
FreeArray(p,row);
system("pause");
}
laomai 2003-11-16
  • 打赏
  • 举报
回复
thanks to Wolf0403(完美废人)
今天才发现我从来没用 free 释放过数组!呵呵。可能是写链表分配一类的程序写多了,而我的动态数组全是用 new 和delete实现的,所以才出了这样的笑话。
感谢sakurar(人脑修理工) 和Wolf0403(完美废人) 两位朋友!
另外,我今天终于把自己的程序上机调试了一下,希望这样写出后的代码再别出什么问题,呵呵。
kuangjingbo 2003-11-16
  • 打赏
  • 举报
回复
malloc();
relloc();
Wolf0403 2003-11-16
  • 打赏
  • 举报
回复
laomai:
忠诚的 C++er 啊,呵呵

free 是函数,接受一个指针作为参数

free(p); 才是。
当然,我也是习惯 delete [] p; 的,但是这是 C,不是 C++,呵呵
laomai 2003-11-16
  • 打赏
  • 举报
回复
to alexzyf()
你分配二维数组的方式有问题啊。我试着按我写的运行一下看看如何?另外,也请你把程序加上注释,以便于大家理解。
laomai 2003-11-16
  • 打赏
  • 举报
回复
呵呵,少了空格
free []p;
这下应该可以了吧,呵呵
sakurar 2003-11-16
  • 打赏
  • 举报
回复
回复人: laomai(老迈) ( ) 信誉:100 2003-11-14 08:47:00 得分:0


改正后的程序如下
void FreeArray(DataType** p,int row)
{
for(int i=0;i<row;i++)
{
free[] p[i];
}
free[]p;
}



free[]???呵呵
alexzyf 2003-11-16
  • 打赏
  • 举报
回复
to laomai
那么使用malloc申请内存时是否有大小限制,我用这个方法写了个程序,运行的时候申请一块较小的内存时,一切正常,但当我申请一块较大的内存是就出错了(申请到的内存地址等于NULL)。然后我用单步调试去执行,这个错误又不再出现,搞得我一头雾水,能否告知原因呢,多谢了!
下面是我的程序的代码:

#include <stdio.h>

int *** createarray(int ***p, int rows, int cols);
int *** freearray(int ***p, int rows, int cols);

int *** createarray(int ***p, int rows, int cols)
{ int i, j;
p=(int ***)malloc(sizeof(int **) * rows);
for( i=0;i<rows;i++)
{ p[i] = (int **)malloc(sizeof(int) * cols );
if (p[i]==NULL)
{ printf("Create array error!!!\n");
exit(1);
}
for ( j=0;j<cols;j++)
{ p[i][j] = (int*)malloc(sizeof(int) * 3 );
if (p[i][j]==NULL)
{ printf("Create array error!!!\n");
exit(1);
}

}
}
return p;
}

int *** freearray(int ***p, int rows, int cols)
{ int i,j;
for( i=0;i<rows;i++)
{ for( j=0;j<cols;j++)
{ free(p[i][j]) ;
}
free(p[i]);
}
free(p);
return NULL;
}


void main()
{ char *filename_in;
char *filename_out;
char ch;
int rows, cols;
int max_col;
FILE *fp_in, *fp_out;
int i, j, k, l, n, nn, nnn;
int ***ppixel;


printf("\nPls input source file name:");
scanf("%s", filename_in);
printf("\n source file name: %s\n", filename_in);
printf("\nPls input destination file name:");
scanf("%s", filename_out);
printf("\n source file name: %s\n", filename_out);

printf("\nPls input number N:");
scanf("%d", &n);

if (n <= 0)
{ printf("\nN is too samll!!!\n");
exit(1);
}
nn = n ;
nnn = 2 * n + 1;

fp_in = fopen(filename_in, "r");
if (fp_in == NULL)
{ printf("Error file in");
return;
}

fp_out = fopen(filename_out, "w");
if (fp_out == NULL)
{ printf("Error file out");
return;
}

do { fputc(ch = fgetc(fp_in), fp_out);
} while(ch != '\n');
do { fputc(ch = fgetc(fp_in), fp_out);
} while(ch != '\n');

fscanf(fp_in, "%d %d\n", &cols, &rows);
fscanf(fp_in, "%d\n", &max_col);
fprintf(fp_out, "%d %d\n", cols, rows);
fprintf(fp_out, "%d\n", max_col);

ppixel = createarray(ppixel, rows, cols);

for (i = 0; i < rows; i ++)
{ for (j = 0; j < cols; j++)
{ fscanf(fp_in, "%d %d %d", &ppixel[i][j][0], &ppixel[i][j][1], &ppixel[i][j][2]);
}
}
for (i = 0; i < (rows/nnn); i++)
for (j = 0; j < (cols/nnn); j++)
for (l = 0; l < nnn; l++)
for (k = 0; k < nnn; k++)
{ ppixel[i * nnn + l][j * nnn + k][0] = ppixel[i * nnn + nn][j * nnn + nn][0];
ppixel[i * nnn + l][j * nnn + k][1] = ppixel[i * nnn + nn][j * nnn + nn][1];
ppixel[i * nnn + l][j * nnn + k][2] = ppixel[i * nnn + nn][j * nnn + nn][2];
}





for (i = 0; i < rows; i ++)
{ for (j = 0; j < cols; j++)
{ fprintf(fp_out, "%4d%4d%4d", ppixel[i][j][0], ppixel[i][j][1], ppixel[i][j][2]);
}
fprintf(fp_out, "\n");
}


ppixel = freearray(ppixel, rows, cols);


fclose(fp_in);
fclose(fp_out);

}
laomai 2003-11-14
  • 打赏
  • 举报
回复
改正后的程序如下
void FreeArray(DataType** p,int row)
{
for(int i=0;i<row;i++)
{
free[] p[i];
}
free[]p;
}

laomai 2003-11-14
  • 打赏
  • 举报
回复
to :alexzyf()
呵呵,你说的对,我是把C++的程序该成C后发的,这个地方忘改了,sorry
alexzyf 2003-11-14
  • 打赏
  • 举报
回复
多谢,但是释放二维数组那一段不是很明白,释放内存不是用free的吗?
zhouqingyuan 2003-11-13
  • 打赏
  • 举报
回复
上面的例子说的很清楚了。
laomai 2003-11-13
  • 打赏
  • 举报
回复
给你一个简单的例子吧

typedef int DataType;
/*建立动态二维数组*/
DataType** CreateArray(DataType** p,int row,int column)
{
p=(DataType**)malloc(sizeof(DataType*)*row);
for(int i=0;i<row;i++)
{
p[i] = (DataType*)malloc(sizeof(DataType)*column);
}
return p;
}
/*处理二维数组 */
DataType** SetArray(DataType**p,int row,int column)
{
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
p[i][j]=i*column+j;
}
return p;
}
void DisplayArray(DataType**p,int row,int column)
{
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
cout<<p[i][j]<<" ";
}
cout<<endl;
}
}
/*释放二维数组*/
void FreeArray(DataType** p,int row)
{
for(int i=0;i<row;i++)
{
delete[] p[i];
}
delete[]p;
}

main()
{
DataType** p; //数组的头指针
int row,column; //行数与列数
scanf("%d",&row);
scanf("%d",&column);
p=CreateArray(p,row,column);
p=SetArray(p,row,column);
p=DisplayArray(p,row,column);
FreeArray(p,row);
system("pause");
}
iceandfire 2003-11-10
  • 打赏
  • 举报
回复
用指针
**二维
***三维
以此类推
sharkhuang 2003-11-10
  • 打赏
  • 举报
回复
大小 和维数没有关系。你就是n维,在计算机里面也是线性存储的!
维数只是你在实现程序上的设计上的概念!
anxing 2003-11-10
  • 打赏
  • 举报
回复
Malloc,Relloc.
fierygnu 2003-11-10
  • 打赏
  • 举报
回复
当然也可以分配数组的整个空间nXmXsizeof(int),然后按照二维数组访问。
fierygnu 2003-11-10
  • 打赏
  • 举报
回复
目前唯一的选择是用二维指针加malloc实现。

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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