简体   繁体   English

设置结构的指针成员,从指针指向结构的指针

[英]Setting a pointer member of a structure, from a pointer to a pointer of the structure

Sorry for the silly title.对不起这个愚蠢的标题。

For part of an (very basic) assignment we're implementing a stack with pointers.对于(非常基本的)分配的一部分,我们正在实现一个带有指针的堆栈。 I'm having a lot of trouble with one small part, so I've isolated it into this small problem.我在一小部分遇到了很多麻烦,所以我将它隔离到这个小问题中。

I will try to explain my problem, but reading the code will probably be easier to understand.我将尝试解释我的问题,但阅读代码可能会更容易理解。

There is a structure (named node) which has 2 members, a char (named data), and a pointer to another node (named next).有一个结构(命名节点),它有 2 个成员、一个字符(命名数据)和一个指向另一个节点的指针(命名为 next)。

Inside the main function I have a pointer named head which points to node1, I want to pass this pointer to another function, and make it point to a new node (and make this new node point to yet another new node).在主 function 内部,我有一个名为 head 的指针,它指向 node1,我想将此指针传递给另一个 function,并使其指向一个新节点(并使这个新节点指向另一个新节点)。 I think I might be okay with setting the pointer to a new node, but I can't correctly get that new node to point to another new node correctly.我想我可能可以将指针设置为一个新节点,但我无法正确地让该新节点正确指向另一个新节点。

#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;
}

I expect to see the output as我希望看到 output 为

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

but instead I get但相反我得到

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

I've been trying to get this to work for ages.多年来,我一直试图让它发挥作用。 I'm thinking that maybe it's to do with creating the nodes in the local scope of modifyPtr and trying to use them in main.我在想这可能与在 modifyPtr 的本地 scope 中创建节点并尝试在 main 中使用它们有关。 But then I don't see why #3 would work.但是我不明白为什么#3会起作用。

Can someone tell me what I'm doing wrong?有人可以告诉我我做错了什么吗? Thanks.谢谢。

void modifyPtr(struct node **p)
{
    struct node n3;
    n3.data = '3';
    ...
    *p = &n3;
}

n3 and n4 are local variables*, so they cease to exists once modifyPtr returns. n3n4是局部变量*,因此一旦modifyPtr返回,它们就不再存在。 You need to allocate them on the heap.您需要在堆上分配它们。

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

You just got lucky that n3.data didn't get clobbered.你很幸运n3.data没有被破坏。

* — Laymen speak. * - 外行人说话。

You're bang-on about the scope.你对 scope 很感兴趣。 The way to explain #3 is that just because it works doesn't mean it always will, and doesn't mean it's right.解释#3 的方式是,仅仅因为它有效并不意味着它总是会,也不意味着它是正确的。 Time to learn dynamic memory allocation: new/delete or malloc/free是时候学习动态 memory 分配:new/delete 或 malloc/free

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM