gpt4 book ai didi

c - 全局结构指针初始化

转载 作者:行者123 更新时间:2023-12-04 05:49:11 24 4
gpt4 key购买 nike

结构定义

struct list
{
struct list **next, **prev;
}

核心文件
//Global struct
struct list *threads = {&threads, &threads}; //Warnings here:

// warning: initialization from incompatible pointer type
// warning: excess elements in scalar initializer
// warning: (near initialization for 'threads')

PS:我没有 main此文件中的函数。这必须是全局性的。

最佳答案

您需要初始化指向结构列表的指针变量 threads带有指向 struct list 的指针. {&threads, &threads}不是指向 struct list 的指针,但它可能是 struct list .

为了定义一个实际的结构实例并获得指向它的指针,您可以使用复合文字并获取其地址:

struct list *threads = &((struct list){&threads, &threads});

(注意:复合字面量 ((type){initializer}) 是 C99 的特性;一些没有达到 13 年前标准的编译器可能会拒绝它。)

关于c - 全局结构指针初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10286347/

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