gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 08:23:08 25 4
gpt4 key购买 nike

我正在使用 g_strsplit 使用 \n 分隔符拆分 msg,并创建一个函数来断开字符串。该函数会中断 msg 并返回调用函数,因此我无法释放被调用函数中的拆分字符串指针。因此试图通过引用传递 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)

关于c - 通过引用外部函数传递 gchar ** 以便在主函数中释放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32484454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com