zoukankan      html  css  js  c++  java
  • malloc动态分配多维数组

    下面试自己写的三个测试程序,如果看懂了基本上动态分配多维数组就没什么问题啦:重点 1:深刻理解多维数组的概念,多维数组在内存中的分配情况,基本上动态分配也没什么问题的。然后还要注意一点的就是,释放是分配的逆过程!!!

    #include <stdio.h>

    #include <malloc.h>

    #include <string.h>

     

    void main(void)

    {

           int i ;

           char (*p)[12] ; //[5][12]

           p = malloc(5 * sizeof(char ) * 12);     //申请5行的地址空间

     

           for(i = 0 ; i < 5 ; i++)

           {

                  strcpy(*(p + i), "hello");

           }

     

           for(i = 0 ; i < 5 ;i++)

           {

                  printf("%s " , *(p + i));

           }

           free(p);

    }

     

    程序2:

    #include <stdio.h>

    #include <malloc.h>

    #include <string.h>

     

    void main(void)

    {

           int i;

           int j ;

           int num = 0 ;

           char **p ;//[5][12]

           p =(char **) malloc( 5 * sizeof(char *) );          

     

           for(i = 0 ; i < 5 ; i++)

           {

                  p[i] = malloc(12 * sizeof(char));

           }

     

           for(j = 0 ; j < 5 ; j++)

           {

                  num = 0 ;

                  for(i = 0 ; i < 12 ; i++)

                  {

                         p[j][i] = 'a' + num;

                         num++ ;

                  }

           }

     

           for(j = 0 ; j < 5 ; j++)

           {

                  for(i = 0 ; i < 12 ; i++)

                  {

                         printf("%c" , p[j][i]);

                  }

                  printf(" ");

           }

     

           for(i = 0 ; i < 5 ; i++)

           {

                  free(p[i]);

           }

           free(p);

    }

     

    程序3:

    #include <stdio.h>

    #include <malloc.h>

    #include <string.h>

     

    void main(void)

    {

           char ***pStr;

           int i , j , k ;

           // 自己分配一个三维的[3][4][5]

          

           pStr = (char ***)malloc(3 * sizeof(char **));      //第一维

          

           //分配二维的

           for(i = 0 ; i < 3 ; i++)

           {

                  *(pStr + i) = malloc(4 * sizeof(char *));

           }

     

           //分配三维

           for(i = 0 ; i < 3 ; i++)

           {

                  for(j = 0 ; j < 4 ; j++)

                  {

                         *(*(pStr + i) + j) = malloc(5 * sizeof(char));

                  }

           }

     

           //使用分配的内存

           for(i = 0 ; i < 3 ; i++)

           {

                  for(j = 0 ; j < 4 ; j++)

                  {

                         for(k = 0 ; k < 5 ; k++)

                         {

                                pStr[i][j][k] = 'a' ;

                                printf("%c" , pStr[i][j][k]);

                         }

                         printf(" ");

                  }

           }

          

           //释放第三维的内存

           for(i = 0 ; i < 3 ; i++)

           {

                  for(j = 0 ; j < 4 ;j++)

                  {

                         free((pStr[i][j]));

                  }

           }

     

           //释放第二维的内存

           for(i = 0 ; i < 3 ; i++)

           {

                  free(pStr[i]);

           }

     

           //释放第一维的内存

           free(pStr);

    }

  • 相关阅读:
    【打印】windows打印控件,Lodop.js介绍
    【MySQL】MySQL查询数据库各表的行数
    【MySQL】MySQL中查询出数据表中存在重复的值list
    【php】php5.0以上,instanceof 用法
    日期转换:Cannot format given Object as a Date (SimpleDateFormat的parse和format)
    Groovy 正则表达式 匹配点号
    什么是开发框架
    SoapUI 增大使用内存
    Groovy API link
    Groovy 跳出each循环
  • 原文地址:https://www.cnblogs.com/bohaoist/p/4607194.html
Copyright © 2011-2022 走看看