Search code examples
cglibc

passing gchar ** by reference to the external function so as to free in main function


I am using g_strsplit to split the msg with \n delimiter and created a function to break the string. The function breaks the msg and return to calling function thus I am not able to free the splitted string pointer in the called function. Thus tried to pass by reference the gchar. However I am getting segmentation fault.

#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;
}

How can I pass the reference of gchar **splitted_string in glib?


Solution

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

    must be changed to

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

    Compiler warns about this with -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)