c - 双星号和 C 中的 `malloc`

标签 c pointers types malloc

我已经研究指针一段时间了,但我似乎无法完全理解它。当您从解释指针的教程跳到假设您了解它们的实际函数和代码时,似乎有一个未解释的差距。 令我烦恼的一段代码如下:

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

好吧,我的理解是这样的,

**output_str is a character
*output_str is a pointer to a character
output_str is a pointer to a pointer to a character

据我所知,malloc() 返回一个指向刚刚分配的内存开始的指针,其大小值等于指向字符 (char*) 的指针的大小。现在,指向内存开头的指针不应该有一个*吗?如果是这样,我们如何将带有 * 的东西分配给 **?我很困惑,如果有人可以提供一些说明,并且可能对内存部分有一些了解,那就太好了。

最佳答案

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

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            |     .....

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

1。当您调用时:

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

操作系统和 C 编译器为我们做了这件事,但我们可以认为我们的内存已被更改。例如。内存地址 3 现在有一个名为 output_strchar pointer to a char pointer。 .

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

2。现在如果我们说:

*output_str = malloc(sizeof(char));

内存又改了。例如。内存地址 0 现在有一个名为 *output_strchar 指针

    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            |     .....

3。我们声明一个静态实例化的字符:

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            |     .....

Lastly, we invoke *output_str = &a; we are now telling char pointer *output_str to point to/reference the previously instantiated 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            |     .....

更多信息

 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" 

关于c - 双星号和 C 中的 `malloc`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42942268/

相关文章:

c - Oracle 上的空(字符串)元素的 XML 验证失败

c - 菜单上的防御性编程。我应该有什么类型的输入?

types - 函数的 Rust HashMap 的类型签名

c - 无法解密函数参数 : pointer and pointer to pointer

错误类型冲突

haskell - 模式匹配 Haskell 列表

c - pthreads 编译的问题

c++ - 混合 c 和 c++ 库(多 Unix 平台)

c++ - C或C++语言中的数组实际上是指针吗

c++ - 如何创建 const 指针的动态数组?