2011-05-02 70 views
0

我正在处理类似这样的事情,但它是抛出错误。如何将数组添加到没有变量的GSList?

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

回答

1

的问题是,编译器不知道你的阵列的类型是什么,所以这样的事情应该工作。

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

但是你应该考虑你怎么这样,这将是更好地使一个单一的静态常量数组,并添加到您的GSList,因为在这里你会被打为O(n ²)因为它必须遍历每个追加的列表。

+0

但是,如果我添加了一个数组不会意味着它会都在同GSlist项目结束了? – 2011-05-02 16:27:43

+0

我收到错误,更新了主帖。 – 2011-05-02 16:33:09

+0

之后我可以使用'g_slist_prepend()'然后'g_slist_reverse()'来解决性能问题,但是我仍然无法运行它。 – 2011-05-02 18:04:39

0

只是试过同样的事情,它的工作原理。 gcc 4.7.1在Debian不稳定的amd64上。 BTW,(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