我有以下代码。
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/