免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 6436 | 回复: 13
打印 上一主题 下一主题

请问动态生成m*n的矩阵?也就是动态的二维数组?拜托了 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-03-29 08:56 |只看该作者 |倒序浏览
比如我可以用:
int * shuzu=(int *)malloc(length*sizeof(int));
用上面的语句可以生成一个length长度的一位整形数组,那么怎么生成
一个m*n的二位这个整形数组呢?这里的m和n都是动态得到的值,拜托大虾了

论坛徽章:
0
2 [报告]
发表于 2006-03-29 09:12 |只看该作者
道理是一样的,因为内存是线性的
int * shuzu=(int *)malloc(m*n*sizeof(int));

论坛徽章:
0
3 [报告]
发表于 2006-03-29 09:22 |只看该作者
int **a=NULL;
a=new int b[];
for(int k=0;k<num;k++)
     {
            b[k]=new int ;
    }

论坛徽章:
0
4 [报告]
发表于 2006-03-29 10:27 |只看该作者
http://www.icylife.net/yunshu/show.php?id=236

以前为别人写过一个一维固定的动态二维数组,和二维全部变化的差不多,你看看。

论坛徽章:
0
5 [报告]
发表于 2006-03-29 14:10 |只看该作者
原帖由 woyaoxuexi 于 2006-3-28 16:56 发表
比如我可以用:
int * shuzu=(int *)malloc(length*sizeof(int));
用上面的语句可以生成一个length长度的一位整形数组,那么怎么生成
一个m*n的二位这个整形数组呢?这里的m和n都是动态得到的值,拜托大虾了


  1. int M = 50;
  2. int N = M;
  3. int **a,i;
  4. a = new int*[M];
  5. for(i = 0;i<M;++i) a[i] = new int[N];
  6. /* use a[i][j] to reference the corresponding element in the array
  7. as if a was a two dimensional array */
复制代码

论坛徽章:
0
6 [报告]
发表于 2006-03-29 16:55 |只看该作者

  1. void *calloc(
  2.    size_t num,
  3.    size_t size
  4. );
复制代码

Allocates an array in memory with elements initialized to 0.

Parameters
num
Number of elements.

size
Length in bytes of each element.

Return Value
calloc returns a pointer to the allocated space. The storage space pointed to by the return value is guaranteed to be suitably aligned for storage of any type of object. To get a pointer to a type other than void, use a type cast on the return value.


  1. /* This program uses calloc to allocate space for
  2. * 40 long integers. It initializes each element to zero.
  3. */
  4. #include <stdio.h>
  5. #include <malloc.h>

  6. int main( void )
  7. {
  8.    long *buffer;

  9.    buffer = (long *)calloc( 40, sizeof( long ) );
  10.    if( buffer != NULL )
  11.       printf( "Allocated 40 long integers\n" );
  12.    else
  13.       printf( "Can't allocate memory\n" );
  14.    free( buffer );
  15. }

复制代码

[ 本帖最后由 westgarden 于 2006-3-29 16:58 编辑 ]

论坛徽章:
0
7 [报告]
发表于 2006-03-30 03:43 |只看该作者
原帖由 依赛特小子 于 2006-3-29 09:22 发表
int **a=NULL;
a=new int b[];
for(int k=0;k<num;k++)
     {
            b[k]=new int ;
    }


这样代码你也能写出来?
a=new int b[];

论坛徽章:
0
8 [报告]
发表于 2006-03-30 09:52 |只看该作者
原帖由 hkwang66 于 2006-3-30 03:43 发表


这样代码你也能写出来?
a=new int b[];


搞不懂,二级数组不就是一维数组吗?和变通的动态内存分配有区别么?偶觉得没有区别……

论坛徽章:
0
9 [报告]
发表于 2006-03-31 21:29 |只看该作者
原帖由 独孤九贱 于 2006-3-30 09:52 发表


搞不懂,二级数组不就是一维数组吗?和变通的动态内存分配有区别么?偶觉得没有区别……

多维数组的确可以使用一维数组表示,比如二维数组
a[n][m]数组的a[k]元素:位置:m*s+k

[ 本帖最后由 hkwang66 于 2006-3-31 22:19 编辑 ]

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
10 [报告]
发表于 2006-03-31 21:56 |只看该作者
好像动态开辟的都没办法使用a[m][n]的方法来指定一个元素了吧?因为编译器并不知道抽象的“维度”。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP