【问题标题】:Setting a pointer member of a structure, from a pointer to a pointer of the structure设置结构的指针成员,从指针指向结构的指针
【发布时间】:2011-05-25 03:10:46
【问题描述】:

对不起这个愚蠢的标题。

对于(非常基本的)赋值的一部分,我们正在实现一个带有指针的堆栈。我在一小部分遇到了很多麻烦,所以我将它隔离到这个小问题中。

我会尝试解释我的问题,但阅读代码可能会更容易理解。

有一个结构(命名节点),它有 2 个成员、一个字符(命名数据)和一个指向另一个节点的指针(命名为 next)。

在主函数内部,我有一个名为 head 的指针,它指向 node1,我想将此指针传递给另一个函数,并使其指向一个新节点(并使这个新节点指向另一个新节点)。我想我可以将指针设置为一个新节点,但我无法正确地让该新节点正确指向另一个新节点。

#include <stdio.h>

struct node {
    char data;
    struct node *next;
};

void modifyPtr(struct node **p);

int main(void)
{
    /* create first 2 nodes */
    struct node n1;
    n1.data = '1';

    struct node n2;
    n2.data = '2';

    /* set 1st node's next node to the 2nd node */
    n1.next = &n2;

    /* create a pointer to a node and make it point to the first node */
    struct node *head = &n1;

    /* this works as expected */
    printf("1. %c\n", head->data);
    printf("2. %c\n", head->next->data);

    /* this should set head to a new node (which in turn points to another new node) */
    modifyPtr(&head);

    /* this prints as expected. Am I just lucky here? */
    printf("3. %c\n", head->data);
    /* but this doesn't. I want it to print 4. */
    printf("4. %c\n", head->next->data);
}

void modifyPtr(struct node **p)
{
    /* create node 3 and 4 */
    struct node n3;
    n3.data = '3';

    struct node n4;
    n4.data = '4';

    /* set node3's next node to node4 */
    n3.next = &n4;

    /* make p point to node 3 */
    *p = &n3;
}

我希望看到输出为

  1. 1
  2. 2
  3. 3
  4. 4

但我得到了

  1. 1
  2. 2
  3. 3
  4. |

多年来,我一直在努力让它发挥作用。我在想这可能与在 modifyPtr 的本地范围内创建节点并尝试在 main 中使用它们有关。但是我不明白为什么 #3 会起作用。

谁能告诉我我做错了什么?谢谢。

【问题讨论】:

    标签: c pointers


    【解决方案1】:
    void modifyPtr(struct node **p)
    {
        struct node n3;
        n3.data = '3';
        ...
        *p = &n3;
    }
    

    n3n4 是局部变量*,因此一旦modifyPtr 返回,它们就不再存在。您需要在堆上分配它们。

    void modifyPtr(struct node **p)
    {
        struct node *pn3 = malloc(sizeof(struct node));
        pn3->data = '3';
        ...
        *p = pn3;
    }
    

    你很幸运 n3.data 没有被破坏。

    * — 外行人说话。

    【讨论】:

    • 非常感谢!我还不能将此设置为已接受的答案,但我很快就会这样做。
    【解决方案2】:

    你对范围很感兴趣。解释#3 的方式是,仅仅因为它有效并不意味着它总是会,也不意味着它是正确的。是时候学习动态内存分配了:new/delete 或 malloc/free

    【讨论】:

    • 谢谢克里斯。当我写这个问题时,范围的事情只是一个短暂的想法。 ikegami 在你之前回答了,所以我会把这个答案作为公认的答案,但是谢谢。
    猜你喜欢
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-21
    • 2012-03-28
    • 2015-01-24
    • 1970-01-01
    相关资源
    最近更新 更多