Advertisement

malloc为二维数组分配内存

阅读量:
复制代码
 int row = 64;

    
 int cloumn = 64;
    
 char **a = (char **)malloc(sizeof(char *) * row);
    
 for (i = 0; i < row; ++i) {
    
     a[i] = (char *)malloc(sizeof(char) * cloumn);
    
 }
    
    
    
    
    AI写代码cpp
复制代码
    int (*a)[2] = malloc(sizeof(int)*3*2);
    
    AI写代码cpp

第三种方式:

使数组元素保持连续存储,但后续在重新分配列时将面临一定挑战,需借助指针运算来实现。

复制代码
 #include <stdio.h>

    
 #include <stdlib.h>
    
 void main()
    
 {
    
      int nrows,ncolumns;
    
      int **Array;
    
      int i,j;
    
      printf("please input nrows&ncolumns:/n");
    
      scanf("%d%d",&nrows,&ncolu

全部评论 (0)

还没有任何评论哟~