c语言之 **指针

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct _proc {
    char **argv;
} PROC;  

void test(PROC * this)
{
    /* 申请指针数组空间 */
    if (!(this->argv = (char**)malloc(sizeof(char *) * 3))) {
        perror("malloc");
        exit(1);
    }
   
    /* argv[0] */
    if (!(this->argv[0] = malloc(0x40))) {
        perror("malloc");
        exit(1);
    }
    printf("this->argv    = %p:%p\n", &this->argv, this->argv);             // 0x7fffd65d9de0:0x5604d873e2a0
    printf("this->argv[0] = %p:%p\n", &this->argv[0], this->argv[0]);       // 0x5604d873e2a0:0x5604d873e2c0

    memcpy(this->argv[0], "ac-test0", 8);
    printf("argv[0]=%s\n", this->argv[0]);
    
    /* argv[1] */
    if (!(this->argv[1] = malloc(0x40))) {
        perror("malloc");
        exit(1);
    }
    printf("this->argv[1] = %p:%p\n", &this->argv[1], this->argv[1]);      // 0x5564a05f42a8:0x5564a05f4720
    memcpy(this->argv[1], "ac-test1", 8);
    printf("argv[0]=%s\n", this->argv[1]);
    
    /* argv[2] */
    if (!(this->argv[2] = malloc(0x40))) {
        perror("malloc");
        exit(1);
    }
    printf("this->argv[2] = %p:%p\n", &this->argv[2], this->argv[2]);      // 0x5564a05f42b0:0x5564a05f4770
    memcpy(this->argv[2], "ac-test2", 8);
    printf("argv[0]=%s\n", this->argv[2]);
     
    /* argv[3] */
    if (!(this->argv[3] = malloc(0x40))) {
        perror("malloc");
        exit(1);
    }   
    printf("this->argv[3] = %p:%p\n", &this->argv[3], this->argv[3]);      // 0x5564a05f42b8:0x5564a05f47c0
    memcpy(this->argv[3], "ac-test3", 8);
    printf("argv[0]=%s\n", this->argv[3]);
    
       free(this->argv);
    free(this->argv[0]);
    free(this->argv[1]);
    free(this->argv[2]);
    free(this->argv[3]);
}

int main()
{
    PROC this = {0};
    
    test(&this);
    
    return 0;
}

相关推荐

  1. c语言 **指针

    2024-06-11 18:58:03       3 阅读
  2. C语言初级指针

    2024-06-11 18:58:03       32 阅读
  3. c语言指针数组

    2024-06-11 18:58:03       12 阅读
  4. 【讨论】C语言提高指针表达式

    2024-06-11 18:58:03       22 阅读

最近更新

  1. 人工智能中的哲学

    2024-06-11 18:58:03       0 阅读
  2. 安装xFormers时遇到的问题,以及正确的安装方式

    2024-06-11 18:58:03       0 阅读
  3. 8个常用的辅助函数!!

    2024-06-11 18:58:03       0 阅读
  4. Cargo

    2024-06-11 18:58:03       0 阅读
  5. Docker从容器打包镜像到本地保存与加载

    2024-06-11 18:58:03       0 阅读
  6. TensorFlow编程环境:构建深度学习的乐园

    2024-06-11 18:58:03       0 阅读

热门阅读

  1. 008.googleNet-FashionMNIST-正确率90.510

    2024-06-11 18:58:03       3 阅读
  2. Android13 Settings 左上角箭头图标点击无效

    2024-06-11 18:58:03       2 阅读
  3. 【摘葡萄game】

    2024-06-11 18:58:03       3 阅读
  4. C++ Primer Chapter 8 The IO Library

    2024-06-11 18:58:03       2 阅读
  5. 深度学习的可微渲染

    2024-06-11 18:58:03       3 阅读
  6. Linux学习问题

    2024-06-11 18:58:03       2 阅读
  7. NAT

    NAT

    2024-06-11 18:58:03      2 阅读
  8. 享元模式

    2024-06-11 18:58:03       2 阅读
  9. css特效:对多个tag标签实现模拟地球仪特效

    2024-06-11 18:58:03       1 阅读
  10. 2024-6-11-SPECT和PET的区别是什么

    2024-06-11 18:58:03       1 阅读
  11. docker-ce 和 docker-ee介绍版本介绍

    2024-06-11 18:58:03       2 阅读
  12. C++中的命令模式

    2024-06-11 18:58:03       1 阅读