C中的双星号和`malloc`

FSh*_*ani 8 c malloc types pointers

我已经学习了一段时间的指针,我似乎无法完全绕过它.当您从解释指向实际函数的指南和假设您了解它们的代码跳转时,似乎存在一个无法解释的差距.令我烦恼的代码如下:

char **output_str = malloc(sizeof(char*));
Run Code Online (Sandbox Code Playgroud)

好的,所以我的理解如下,

**output_str is a character
*output_str is a pointer to a character
output_str is a pointer to a pointer to a character
Run Code Online (Sandbox Code Playgroud)

据我所知,malloc()返回一个指向刚刚分配的内存开头的指针,该指针的大小值等于指向字符(char*)的指针的大小.现在,不应该指向内存开始的指针有一个*?如果是这样,我们怎样才能为*分配*?我很困惑,如果有人能提供一些澄清,并且可能对内存部分有一些了解,那将非常好.

San*_*ela 4

您的代码块是正确的。有了这个声明:

char **output_str = malloc(sizeof(char*));

output_str 是一个指向 char 指针的 char 指针,或者可以将其视为 char 的二维数组,或 char 的矩阵。

以图形表示:

Memory Address | Stored Memory Address Value
----------------------------------------------
0              |     .....
1              |     .....
2              |     .....
3              |     .....
4              |     .....
5              |     .....
6              |     .....
.              |     .....
.              |     .....
.              |     .....
n-1            |     .....
Run Code Online (Sandbox Code Playgroud)

内存想象成一个非常大的数组,您可以通过其内存地址访问其中的位置(在本例中,我们已将地址简化为自然数。实际上,它们是十六进制值)。“n”是内存的总量(或大小)。由于 Memory 从 0 开始计数,因此大小相当于 n-1。

1. 当您调用时:

char **output_str = malloc(sizeof(char*));

操作系统和C编译器为我们做了这件事,但我们可以认为我们的内存已经被改变了。例如,内存地址 3 现在有一个char 指针,指向名为 的char 指针output_str。。

    Memory Address | Name - Stored Memory Address Value (it points to ...)
    -----------------------------------------------------
    0              |     .....
    1              |     .....
    2              |     .....
    3              |     output_str = undefined
    4              |     .....
    5              |     .....
    6              |     .....
    .              |     .....
    .              |     .....
    .              |     .....
    n-1            |     .....
Run Code Online (Sandbox Code Playgroud)

2. 现在如果我们说:

*output_str = malloc(sizeof(char));

记忆又被改变了。例如,内存地址 0 现在有一个名为 的char 指针*output_str

    Memory Address | Name - Stored Memory Address Value (it points to ...)
    -----------------------------------------------------
    0              |     *output_str = undefined
    1              |     .....
    2              |     .....
    3              |     output_str = 0
    4              |     .....
    5              |     .....
    6              |     .....
    .              |     .....
    .              |     .....
    .              |     .....
    n-1            |     .....
Run Code Online (Sandbox Code Playgroud)

3. 我们声明一个静态实例化的 char:

char a = 'a';

所以我们的内存再次发生了变化,它被放置在 MemoryAddress[6] = 'a' 处:

        Memory Address | Name -> Stored Memory Address Value (it points to ...)
        ------------------------------------------------------
        0              |     *output_str = undefined
        1              |     .....
        2              |     .....
        3              |     output_str = 0
        4              |     .....
        5              |     .....
        6              |     a = 'a' // 'a'is static memory
        .              |     .....
        .              |     .....
        .              |     .....
        n-1            |     .....
Run Code Online (Sandbox Code Playgroud)

最后,我们调用*output_str = &a;我们现在告诉 char 指针*output_str指向/引用先前实例化的char a.

所以我们最终的记忆会是这样的:

            Memory Address | Name - Stored Memory Address Value (it points to ...)
            -----------------------------------------------------
            0              |     *output_str = 6
            1              |     .....
            2              |     .....
            3              |     output_str = 0
            4              |     .....
            5              |     .....
            6              |     a = 'a' // 'a'is static memory
            .              |     .....
            n-1            |     .....
Run Code Online (Sandbox Code Playgroud)

更多信息

 Now printf("Value: " + a) will output "Value: a" 
 printf("Value: " + *output_str[0]) will also output "Value: a" 
 And printf("Value: " + **output_str) will output "Value: a" 
Run Code Online (Sandbox Code Playgroud)