gpt4 book ai didi

c - 如何在没有变量的情况下将数组添加到 GSList?

转载 作者:行者123 更新时间:2023-11-30 16:01:54 25 4
gpt4 key购买 nike

我正在处理类似的事情,但是它会抛出错误。

我只是将数组放入一个变量中并以这种方式传递它,但我正在查看近 500 行,例如无模式数据的行。 (所以我不能使用循环)

此外,使用 GSList 的全部目的是避免锯齿状数组的限制

list43333 = g_slist_append(list43333,{11,12,13,14,15,17,18,20,22,25,30});

编辑:使用`(int[])进行转换给出:

csgtk.h:14: warning: data definition has no type or storage class
csgtk.h:14: warning: type defaults to ‘int’ in declaration of ‘list43333’
csgtk.h:14: error: conflicting types for ‘list43333’
csgtk.h:12: note: previous definition of ‘list43333’ was here
csgtk.h:14: warning: passing argument 1 of ‘g_slist_append’ makes pointer from integer without a cast
/usr/include/glib-2.0/glib/gslist.h:52: note: expected ‘struct GSList *’ but argument is of type ‘int’
csgtk.h:14: warning: initialization makes integer from pointer without a cast
csgtk.h:14: error: initializer element is not constant

编辑:文字复制粘贴以显示它没有超出范围(注意,这是在 .h 文件的顶层):

GSList * list43333 = NULL;
list43333 = g_slist_prepend(list43333,(int[]){});

主文件

#include "csgtk.h"

GHashTable * widgetbuffer;
[...]

最佳答案

问题是编译器不知道数组的类型是什么,所以这样的东西应该可以工作。

list43333 = g_slist_append(list43333,(int[]){11,12,13,14,15});

但是您应该考虑如何执行此操作,最好创建一个静态常量数组并将其添加到您的 GSList 中,因为在这里您将遇到 O( n²) 类型的运行时间,因为它必须遍历每个追加的列表。

关于c - 如何在没有变量的情况下将数组添加到 GSList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5857415/

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