我用glib-2.0连接两个简单的字符串。这是我的代码:

int main() {

    gchar *a = "12";
    gchar *b = "1231212";

    printf("a: %s\n", a);
    printf("b: %s\n", b);
    gchar *c = g_strjoin(",", a, b);
    printf("c: %s\n", c);

    return EXIT_SUCCESS;
}

它在g_strjoin(",", a, b)时崩溃。核心文件不显示太多信息:
Using host libthread_db library "/lib64/libthread_db.so.1".
Core was generated by `./server'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  strlen () at ../sysdeps/x86_64/strlen.S:137
137     FIND_ZERO

我做错什么了吗?我在用20号软呢帽。

最佳答案

由于g_strjoin()可以连接任意数量的字符串,因此列表必须以NULL结尾:

 gchar *c = g_strjoin(",", a, b, NULL );

关于c - 连接两个字符串时g_strjoin崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25744275/

10-17 02:48