• c语言指针的指针


    1、情况

    c语言指针的指针,还是比较常用的一个功能;当然,我也相信,一些用C语言很长时间的人,也没大用过,因为用不到,这是工作需求决定的,但总体来说,还是经常用的。
    理解了指针的指针,我感觉才是真正理解了指针的含义

    2、定义

    指向指针的指针是一种多级间接寻址的形式,或者说是一个指针链。通常,一个指针包含一个变量的地址。当我们定义一个指向指针的指针时,第一个指针包含了第二个指针的地址,第二个指针指向包含实际值的位置。

    C 中指向指针的指针
    一个指向指针的指针变量必须如下声明,即在变量名前放置两个星号。例如,下面声明了一个指向 int 类型指针的指针:
    int **var;


     3、失败的实例

    1. 1 #include
    2.   2 #include
    3.   3 #include
    4.   4 #include
    5.   5 
    6.   6 void getMemory(char *p, int num)
    7.   7 {
    8.   8  printf("enter function getMemory\r\n");
    9.   9  printf("p=%p,&p=%p\r\n", p, &p);
    10.  10  p = (char *)malloc(sizeof(char) * num);
    11.  11  printf("p=%p,&p=%p\r\n", p, &p);
    12.  12  printf("exit function getMemory\r\n");
    13.  13 }
    14.  14 
    15.  15 int main()
    16.  16 {
    17.  17   char *str = NULL;
    18.  18   printf("str=%p,&str=%p\r\n", str, &str);
    19.  19   getMemory(str, 100);
    20.  20   strcpy(str, "hello");
    21.  21   printf("str=%s\r\n", str);
    22.  22   printf("str=%p\r\n", str);
    23.  23   printf("&str=%p\r\n", &str);
    24.  24   free(str);
    25.  25 
    26.  26 }
    27. ~                                                                                                                                                                                                                                                                                
    28. ~                                                                                                                                                                                              
    29. "test2.c" 26L, 538C 已写入             
    30. root@mkx:~/learn/getMemory# ./test2
    31. str=(nil),&str=0x7ffd24ae73c0
    32. enter function getMemory
    33. p=(nil),&p=0x7ffd24ae73a8
    34. p=0x6a9420,&p=0x7ffd24ae73a8
    35. exit function getMemory
    36. 段错误 (核心已转储)
    37. root@mkx:~/learn/getMemory# 

    4、成功的实例

    1.  1 #include
    2.   2 #include
    3.   3 #include
    4.   4 #include
    5.   5 
    6.   6 void getMemory(char **p, int num)
    7.   7 {
    8.   8  printf("enter function getMemory\r\n");
    9.   9  printf("p=%p,*p=%p\r\n", p, *p);
    10.  10  *p = (char *)malloc(sizeof(char) * num);
    11.  11  printf("p=%p,*p=%p\r\n", p, *p);
    12.  12  printf("exit function getMemory\r\n");
    13.  13 }
    14.  14 
    15.  15 int main()
    16.  16 {
    17.  17   char *str = NULL;
    18.  18   printf("str=%p, &str=%p\r\n", str, &str);
    19.  19   getMemory(&str, 100);
    20.  20   strcpy(str, "hello");
    21.  21   printf("str=%s\r\n", str);
    22.  22   printf("str=%p\r\n", str);
    23.  23   printf("&str=%p\r\n", &str);
    24.  24   free(str);
    25.  25 
    26.  26 }
    27. ~                                                                                                                                                                                              
    28.                                                                                                                                                                                                                                                                                 
    29. ~                                                                                                                                                                                              
    30. "test1.c" 26L, 542C            
    31. root@mkx:~/learn/getMemory# gcc test1.c -o test1
    32. root@mkx:~/learn/getMemory# ./test1 
    33. str=(nil), &str=0x7ffeddf9e010
    34. enter function getMemory
    35. p=0x7ffeddf9e010,*p=(nil)
    36. p=0x7ffeddf9e010,*p=0xf22420
    37. exit function getMemory
    38. str=hello
    39. str=0xf22420
    40. &str=0x7ffeddf9e010

     5、最后的总结

    失败的例子的情况是这样的:

    失败就是失败在,传给函数参数的变量p,与当前变量str的地址已经不一样了,它们只是存储的内容是一样的,这就决定了两边的操作,已经没有任何关系了,后来,又给没有分配内存的变量赋值,程序肯定崩溃了

    成功的例子情况是这样的:

    这里的成功之处,就在于用了指针的指针,一想,感觉有些糊涂的感觉,细想一下,其根本之处在于通过第二级指针,准确的定位到了一级指针,给一级指针赋值了分配内存的地址,就是这么简单。

  • 相关阅读:
    qnx sh: rm: Arg list too long
    matlab GPR高斯过程回归与股票价格预测
    客户文章|南方医科大学李克玄团队破解肠道宏病毒与心肌病关系
    Java中如何实现定时任务?
    爬虫之Scrapy框架
    一种指纹生物特征的隐私保护和认证方法
    文心一言 VS 讯飞星火 VS chatgpt (108)-- 算法导论10.1 6题
    TypeScript 笔记:String 字符串
    postman接口测试系列: 时间戳和加密
    【考研英语语法】名词性从句
  • 原文地址:https://blog.csdn.net/maokexu123/article/details/126309733