C语言中如何定义一个二维数组,数组长度由输入值确定?

[复制链接]
查看11 | 回复4 | 2017-12-16 11:06:06 | 显示全部楼层 |阅读模式
可以通过malloc()函数动态分配,示例://---------------------------------------------------------------------------#include #include typedef int datatype;datatype **na(const int m,const int n)/*创建一个m行n列的datatype类型数组,返回首地址,如果创建失败则返回NULL*/{ int i; datatype **a=(datatype**)malloc(sizeof(datatype *)*m);
回复

使用道具 举报

千问 | 2017-12-16 11:06:06 | 显示全部楼层
intmain(){intiLen;int*p=NULL;inti;scanf("%d",&iLen);if(iLen<=0){printf("数组大小不能小于1!\n");return-1;}p=(int*)malloc(iLen*sizeof(int));if(p
回复

使用道具 举报

千问 | 2017-12-16 11:06:06 | 显示全部楼层
int main(){ int iLen; int *p=NULL; int i; scanf("%d",&iLen); if(iLen <= 0) {
printf("数组大小不能小于1!\n");
return -1; } p= (int *) malloc(
回复

使用道具 举报

千问 | 2017-12-16 11:06:06 | 显示全部楼层
不可以,数组在定义的时候必须先给定大小。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行