gpt4 book ai didi

C 中的交叉引用结构

转载 作者:行者123 更新时间:2023-11-30 18:13:58 25 4
gpt4 key购买 nike

有没有办法创建两个相互引用的结构?示例:

struct str1
{
struct str1* ptr1;
struct str2* ptr2;
}

struct str2
{
struct str1* ptr1;
struct str2* ptr2;
}

最佳答案

struct str2; // put a forward reference to str2 here

struct str1
{
struct str1* s1;
struct str2* s2;
};

struct str2
{
struct str1* s1;
struct str2* s2;
};

int main()
{
struct str1 s1;
struct str2 s2;

s1.s1 = &s1;
s1.s2 = &s2;
s2.s1 = &s1;
s2.s2 = &s2;

return 0;
}

关于C 中的交叉引用结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20341335/

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