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

时间:2011-05-02 12:58:26

标签: c arrays glib singly-linked-list

我正在使用类似的东西,但这会引发错误。

我只是将数组放在一个变量中并以这种方式传递它,但我正在查看近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;
[...]

2 个答案:

答案 0 :(得分:1)

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

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

然而你应该考虑你是怎么做的,最好制作一个静态常量数组并将其添加到GSList,因为在这里你将会遇到O(n²)类运行时间,因为它必须遍历每个追加的列表。

答案 1 :(得分:0)

尝试过同样的事情,但它确实有效。关于Debian unstable amd64的gcc 4.7.1。顺便说一下,(int []){1, 2, 3}是ISO C99复合文字。

#include <glib.h>

int main()
{
        GSList *l;

        l = g_slist_alloc();
        l = g_slist_append(l, (int []){1, 2, 3});

        return 0;
}

$ gcc -Wall -Wextra -g $(pkg-config --cflags --libs glib-2.0) main.c

相关问题