C分配指针?这是什么?

标签 c pointers memory-management char

我有以下代码。

char a[] = "abcde";
char *b = "fghij";
char *c = malloc(6);
char *d = c;
c = "klmno";

练习中指出:

Draw a picture of the data structures a, b, c and d(with content), where you can see what has been allocated and use arrows to show how pointers are set.

我的解决方案是:

      ____________
a -> |a|b|c|d|e|\0|
      ¨¨¨¨¨¨¨¨¨¨¨¨
      ____________
b -> |f|g|h|i|j|\0|
      ¨¨¨¨¨¨¨¨¨¨¨¨
      ____________
c -> |k|l|m|n|o|\0|
      ¨¨¨¨¨¨¨¨¨¨¨¨
      ___________
d -> | | | | | | |
      ¨¨¨¨¨¨¨¨¨¨¨

但是我的解决方案没有被接受,响应是“为指向 b、c、d 但不是 a 的指针分配内存”。谁能解释一下这是什么意思?

最佳答案

这是一个有点神秘的回答,但我想投诉是数组 a 没有指向那个数据;它包含数据。

所以这可能是所需要的(没有指针箭头):

    ____________
a  |a|b|c|d|e|\0|
    ¨¨¨¨¨¨¨¨¨¨¨¨

数组的名称可以用作指针,但它仍然具有微妙的不同含义。

关于C分配指针?这是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8340708/

相关文章:

c - 如何输出使用 ANTLR 构建的 AST?

C:比较hash值好像消失了

c - 对 `log' 的 undefined reference

unix - dlmalloc 如何合并 block ?

ios - 推送 View Controller 中的内存泄漏

c++ - Netbeans c/c++ 远程执行

c++ - 确保const指针成员变量的constness

c - QSorting a malloc'd array of structures?

c - 链表不打印任何内容

c++ - 将 RAII 与字符指针一起使用