通过引用外部函数传递gchar **以便在main函数中释放

时间:2015-09-09 16:08:26

标签: c glibc

我正在使用g_strsplit将msg与\n分隔符分开,并创建了一个断开字符串的函数。该函数打破了msg并返回调用函数,因此我无法释放被调用函数中的splitted字符串指针。因此试图通过引用传递gchar。但是我得到了分段错误。

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

int split_message_syslog_forwarder(char **msg_full,gchar **splitted_strings)
{
    int msg_length = -1;
    *splitted_strings = g_strsplit(*msg_full, "\n", 2);
    if (*splitted_strings != NULL)
    {
        sscanf(*splitted_strings[0], "%d", &msg_length);
        if(msg_length<0)
        {
            *msg_full =  *splitted_strings[1];
        }
    }  
    return msg_length;
}

int main()
{
    int msg_length = -1;
    char *msg_full = "12\nwhat is this";
    gchar **splitted_strings;
    int ret = split_message_syslog_forwarder(&msg_full,&splitted_strings);
    printf("spilitted msg = %d",ret);
    printf("spilitted msg 2= %s",msg_full);
    return 0;
}

如何在glib中传递gchar **splitted_string的引用?

1 个答案:

答案 0 :(得分:2)

int split_message_syslog_forwarder(char **msg_full,gchar **splitted_strings)

必须更改为

int split_message_syslog_forwarder(char **msg_full,gchar ***splitted_strings)

编译器使用-Wall

警告此事
xyz.c:5:5: note: expected 'gchar ** {aka char **}' but argument is of type 'gchar *** {aka char ***}'
 int split_message_syslog_forwarder(char **msg_full,gchar **splitted_strings)