Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C调用链表内容的函数_C_Linked List_Sdl_Glib - Fatal编程技术网

C调用链表内容的函数

C调用链表内容的函数,c,linked-list,sdl,glib,C,Linked List,Sdl,Glib,我正在使用GLib管理链接列表。我声明了2个结构,并将它们放置在一个链表中,如下所示 Asteroid asteroid = {0,0,50,50,50} Asteroid asteroids = {0,0,200,200,50}; GList *asteroidList = NULL; asteroidList = g_list_append(asteroidList, &asteroid); asteroidList = g_list_append(asteroidList, &a

我正在使用GLib管理链接列表。我声明了2个结构,并将它们放置在一个链表中,如下所示

Asteroid asteroid = {0,0,50,50,50}
Asteroid asteroids = {0,0,200,200,50};

GList *asteroidList = NULL;
asteroidList = g_list_append(asteroidList, &asteroid);
asteroidList = g_list_append(asteroidList, &asteroids);
然后我使用下面的函数遍历列表并调用一个函数,该函数将结构作为一个圆圈绘制到屏幕上,如下所示

void drawAsteroids(){
GList *list = asteroidList;
while(list != NULL){
    printf("Asteroids");
    GList *next = list->next;
    drawAsteroid(list->data);
    list = next;
  }
}
typedef struct asteroid{
    int xSpeed;
    int ySpeed;

    int xPos;
    int yPos;

    int r;
}Asteroid;
绘图功能是

void drawAsteroid(void *asteroid){
    Asteroid *newAsteroid = (Asteroid *)asteroid;
    printf("%d\n", newAsteroid->xPos);
    circleRGBA(renderer, newAsteroid->xPos, newAsteroid->yPos, newAsteroid->r, 0, 255, 0, 255);
}
结构的定义如下

void drawAsteroids(){
GList *list = asteroidList;
while(list != NULL){
    printf("Asteroids");
    GList *next = list->next;
    drawAsteroid(list->data);
    list = next;
  }
}
typedef struct asteroid{
    int xSpeed;
    int ySpeed;

    int xPos;
    int yPos;

    int r;
}Asteroid;

当我运行此代码时,我看不到任何绘制到屏幕上的内容,
GList
函数不会复制数据。如果将
Asteroid
结构放在堆栈上,并且它超出范围,那么
list->data
指针将指向垃圾


您需要在堆上分配数据,然后记得在不再需要的时候释放它。

遵循这一点,但仍然没有,并且仍然使用重复的变量名。这感觉像是第一个问题……我将链表迁移到Glib,而不是我使用的第一个库,并创建了我自己的函数来遍历列表,并对列表的内容调用该函数列表需要更多信息吗?请阅读有关MCVE的链接。你的例子不完整。堆栈上的两个
Asteroid
变量可能需要在堆上分配,但是如果没有一个最小但可编译的程序,我们就无法判断。