gpt4 book ai didi

c - C中指向对象和指针的区别(如何指向另一个指针)

转载 作者:行者123 更新时间:2023-11-30 18:31:37 24 4
gpt4 key购买 nike

如何从另一个结构对象指向一个结构对象?

这是 .h 文件:

#include <stdio.h>
#include <stdlib.h>

typedef struct
{
int num;
struct node *next;

} talstrul;

这是在 .c 文件中:

talstrul obj1;
talstrul obj2;

现在我希望 obj1 的指针指向 obj2 的指针。所以我尝试这样做:

obj1.next = &obj2;

但是我收到一个错误:

'=' : incompatible types - from 'talstrul' to 'node *'

最佳答案

您收到此错误是因为您试图将 obj1 的指针指向 obj2,而不是指向 obj2 的指针。

如果您希望 obj1 的指针都指向 obj2 的指针,则必须将 next 从 node * 更改为 talstrul *,它可能是更容易使用

struct talstrul {
int num;
talstrul* next;
};

还有

obj1.next = obj2.next;

obj1.next = &obj2

如果您希望 obj1.next 实际上指向 obj2,而不仅仅是指向 obj2 的指针。

关于c - C中指向对象和指针的区别(如何指向另一个指针),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21762407/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com