Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.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 glib-单链表_C_Glib_Singly Linked List - Fatal编程技术网

C glib-单链表

C glib-单链表,c,glib,singly-linked-list,C,Glib,Singly Linked List,我试图进入glib库,所以我想我会尝试做一些简单的程序来实现glib的单链表。 在我尝试使用2个函数之前,一切都很正常: g_slist_n和g_slist_n数据。 我不明白我做错了什么。也许你能帮我把事情看清楚。相关行为:64-70。 我是这样编译的: gcc单链接列表.cpkg config--cflags--libs glib-2.0 多谢各位 #include <stdio.h> #include <stdlib.h> #include <string.h

我试图进入glib库,所以我想我会尝试做一些简单的程序来实现glib的单链表。 在我尝试使用2个函数之前,一切都很正常: g_slist_n和g_slist_n数据。 我不明白我做错了什么。也许你能帮我把事情看清楚。相关行为:64-70。 我是这样编译的:

gcc单链接列表.c
pkg config--cflags--libs glib-2.0

多谢各位

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>

struct person {
    char *name;
    int age;
};

struct person *new_person(char *name, int age);
void free_person(gpointer person);
void printf_person(gpointer person, gpointer data);
void printf_int(void *integer, void *formatstring);


int main(void) {
    GSList *list_int, *list_people;

    // EXAMPLE 1
    // Create a sample list of int
    for (int i = 5; i > 0; i--) {
        list_int = g_slist_prepend(list_int, GINT_TO_POINTER(i));
    }
    // print list
    printf("===== list of integers =====\n\n");
    g_slist_foreach(list_int, printf_int, "Node %d: int = %d\n"); 
    // free list
    g_slist_free(list_int);

    printf("\n");

    // EXAMPLE 2
    // create a sample list of people
    for (int i = 1; i <= 5; i++) {
        struct person *new_p = new_person(NULL, 0);
        char *name = (char *) malloc(11 * sizeof(char)); //+1 for \0

        if (new_p && name) {
            char num[sizeof(int)]; // is this a valid hack?

            // convert int to string
            sprintf(num, "%d", i);
            // setup the name
            strcpy(name, "Person ");    
            strcat(name, num);

            // set new_person data
            new_p->name = name;
            new_p->age = i * 9; // just vary the age

            list_people = g_slist_append(list_people, (gpointer) new_p);
        } else {
            if(new_p)
                free(new_p);
            else
                free(name);
        }
    }
    // print list
    printf("\n===== list of people =====\n\n");
    g_slist_foreach(list_people, printf_person, NULL);

    // WHY DOESNT THIS WORK?
    // gpointer data = g_slist_nth_data(list_people, 1);
    // printf_person(data, NULL);

    // NEITHER DOES THIS
    // GSList *node = g_slist_nth(list_people, 0);
    // printf_person(node->data, NULL);

    // free list
    g_slist_free_full(list_people, free_person);

    return 0;
}

// Allocates space for a new struct person
struct person *new_person(char *name, int age) {
    struct person *new_person = (struct person *) malloc(sizeof(struct person));
    if (new_person) {
        new_person->name = name;
        new_person->age = age;
    }

    return new_person;
}

// frees the allocated space for struct person and its data
void free_person(gpointer person) {
    free(((struct person *) person)->name);
    free((struct person *) person);
}   

// print function for person
void printf_person(gpointer person, gpointer unused) {
    printf("Name: %s\nAge: %d\n\n", ((struct person *) person)->name, ((struct person *) person)->age);
}

// print function for int
void printf_int(gpointer integer, gpointer formatstring) {
    static int ctr = 1;

    printf((char *) formatstring, ctr++, GPOINTER_TO_INT(integer)); 
}   
#包括
#包括
#包括
#包括
结构人{
字符*名称;
智力年龄;
};
结构人员*新人员(字符*姓名,整数);
无效自由人(gpointer person);
无效打印人员(gpointer人员、gpointer数据);
void printf_int(void*整数,void*格式字符串);
内部主(空){
GSList*list\u int,*list\u人员;
//例1
//创建int的示例列表
对于(int i=5;i>0;i--){
list_int=g_slist_prepend(list_int,GINT_TO_指针(i));
}
//打印列表
printf(“===整数列表====\n\n”);
g_slist_foreach(list_int,printf_int,“节点%d:int=%d\n”);
//免费名单
g_slist_free(list_int);
printf(“\n”);
//例2
//创建人员的示例列表
对于(int i=1;i name=name;
new_p->age=i*9;//只需改变年龄即可
list_people=g_slist_append(list_people,(gpointer)new_p);
}否则{
如果(新的)
免费(新);;
其他的
免费(姓名);
}
}
//打印列表
printf(“\n===人员列表===\n\n”);
g_slist_foreach(列出人员,打印人员,空);
//为什么这不起作用?
//gpointer data=g_slist_n_数据(列出人员,1);
//打印人员(数据,空);
//这也不是
//GSList*node=g\u slist\n(list\u people,0);
//printf_person(节点->数据,空);
//免费名单
g_slist_free_full(列出人、自由人);
返回0;
}
//为新结构人分配空间
结构人员*新人员(字符*姓名,整数年龄){
结构人*new_person=(结构人*)malloc(sizeof(结构人));
如果(新员工){
新建人员->姓名=姓名;
新人->年龄=年龄;
}
返回新的用户;
}
//释放为struct person及其数据分配的空间
无效自由人(gpointer人){
自由(((结构人*)人)->姓名;
自由((结构人*)人);
}   
//个人打印功能
无效打印人员(gpointer人员,gpointer未使用){
printf(“姓名:%s\n姓名:%d\n\n”,((结构人*)姓名)->姓名,((结构人*)年龄)->;
}
//int的打印函数
void printf_int(gpointer整数,gpointer格式字符串){
静态int ctr=1;
printf((char*)格式字符串,ctr++,GPOINTER_TO_INT(整数));
}   

侧面:除非伏都教以某种方式参与进来,否则我看不出g_slist_prepend(list_int,…)是如何工作的。
list_int
是一个不确定的指针,传递给一个函数,这个函数肯定会取消引用它。如果它不调用未定义的行为,我会感到震惊。同样,对于
g_slist_append(list_people,…)
。我见过的所有使用slist函数的示例都将其指针初始化为NULL。你也应该这样做。@WhozCraig我按照你的建议添加了一行:list\u int=list\u people=NULL;现在它工作得很好。谢谢:)