• 第34课 多维数组和多维指针


    指向指针的指针:

    问题:

    重置动态空间大小示例:

     1 #include <stdio.h>
     2 #include <malloc.h>
     3 
     4 int reset(char**p, int size, int new_size)
     5 {
     6     int ret = 1;
     7     int i = 0;
     8     int len = 0;
     9     char* pt = NULL;
    10     char* tmp = NULL;
    11     char* pp = *p;
    12     
    13     if( (p != NULL) && (new_size > 0) )
    14     {
    15         pt = (char*)malloc(new_size);
    16         
    17         tmp = pt;
    18         
    19         len = (size < new_size) ? size : new_size;
    20         
    21         for(i=0; i<len; i++)
    22         {
    23             *tmp++ = *pp++;      
    24         }
    25         
    26         free(*p);
    27         *p = pt;
    28     }
    29     else
    30     {
    31         ret = 0;
    32     }
    33     
    34     return ret;
    35 }
    36 
    37 int main()
    38 {
    39     char* p = (char*)malloc(5);
    40     
    41     printf("%p
    ", p);
    42     
    43     if( reset(&p, 5, 3) )
    44     {
    45         printf("%p
    ", p);
    46     }
    47 
    48     free(p);
    49     
    50     return 0;
    51 }

    运行结果如下:

    二维数组与二维指针:

    遍历二维数组:

     1 #include <stdio.h>
     2 #include <malloc.h>
     3 
     4 void printArray(int a[], int size)
     5 {
     6     int i = 0;
     7     
     8     printf("printArray: %d
    ", sizeof(a));
     9 
    10     for(i=0; i<size; i++)
    11     {
    12         printf("%d
    ", a[i]);
    13     }
    14 }
    15 
    16 int main()
    17 {
    18     int a[3][3] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}};
    19     int* p = &a[0][0];
    20     
    21     int i = 0;
    22     int j = 0;
    23     
    24     for(i=0; i<3; i++)
    25     {
    26         for(j=0; j<3; j++)
    27         {
    28             printf("%d, ", *(*(a+i) + j));
    29         }
    30         
    31         printf("
    ");
    32     }
    33     
    34     printf("
    ");
    35     
    36     printArray(p, 9);
    37     
    38     return 0;
    39 }

    第28行的等价代换如下:

    10-13行的程序是线性的访问一段内存的值。

    运行结果如下:

    二位数组在内存中是按一维的方式排列的。

    数组名:

    动态申请二维数组:

     1 #include <stdio.h>
     2 #include <malloc.h>
     3 
     4 int** malloc2d(int row, int col)
     5 {
     6     int** ret = NULL;
     7     
     8     if( (row > 0) && (col > 0) )
     9     {
    10         int* p = NULL;
    11         
    12         ret = (int**)malloc(row * sizeof(int*));
    13         p = (int*)malloc(row * col * sizeof(int));
    14         
    15         if( (ret != NULL) && (p != NULL) )
    16         {
    17             int i = 0;
    18             
    19             for(i=0; i<row; i++)
    20             {
    21                 ret[i] = p + i * col;
    22             }
    23         }
    24         else
    25         {
    26             free(ret);
    27             free(p);
    28             
    29             ret = NULL;
    30         }
    31         
    32     }
    33     
    34     return ret;
    35 }
    36 
    37 void free2d(int** p)
    38 {
    39     if( *p != NULL )
    40     {
    41         free(*p);
    42     }
    43     
    44     free(p);
    45 }
    46 
    47 int main()
    48 {
    49     int** a = malloc2d(3, 3);
    50     int i = 0;
    51     int j = 0;
    52     
    53     for(i=0; i<3; i++)
    54     {
    55         for(j=0; j<3; j++)
    56         {
    57             printf("%d, ", a[i][j]);
    58         }
    59         
    60         printf("
    ");
    61     }
    62     
    63     free2d(a);
    64     
    65     return 0;
    66 }

    运行结果如下:

    小结:

  • 相关阅读:
    logback不输出日志消息,且SLF4J绑定源错误
    solr6.4.1 搜索引擎(1)启动eclipse启动
    centos7网络连接
    OutOfMemoryError异常java内存泄漏(Memory Leak)和内存溢出(Memory Overflow)
    centos7软件安装redis3.2
    centos7软件安装mysql5.7
    solr6.4.1搜索引擎(5)使用zookeeper3.4.9分布式集群
    mysql优化数据库优化、SQL优化
    solr6.4.1搜索引擎(4)tomcat重启后数据加载缓慢或丢失
    centos7软件安装jdk1.8
  • 原文地址:https://www.cnblogs.com/wanmeishenghuo/p/9545196.html
Copyright © 2020-2023  润新知