通过引用外部函数传递 gchar ** 以便在主函数中释放
passing gchar ** by reference to the external function so as to free in main function
我正在使用 g_strsplit
使用 \n
分隔符拆分消息,并创建了一个函数来断开字符串。该函数将 msg 和 return 分解为调用函数,因此我无法释放被调用函数中的拆分字符串指针。因此试图通过引用传递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
的引用?
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)
我正在使用 g_strsplit
使用 \n
分隔符拆分消息,并创建了一个函数来断开字符串。该函数将 msg 和 return 分解为调用函数,因此我无法释放被调用函数中的拆分字符串指针。因此试图通过引用传递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
的引用?
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)