从接受两个字符串的C函数返回字符串
原谅我的无知!我试图创建一个函数,它接受两个字符数组作为参数,并返回一些JSON。这是我的代码,后面是编译警告。程序只是在执行时对故障进行分段从接受两个字符串的C函数返回字符串,c,compiler-errors,C,Compiler Errors,原谅我的无知!我试图创建一个函数,它接受两个字符数组作为参数,并返回一些JSON。这是我的代码,后面是编译警告。程序只是在执行时对故障进行分段 #include <stdio.h> #include <stdlib.h> #include <string.h> char get_json(char *sid, char *obuf) { char *json; json = malloc(strlen(obuf)+37);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char get_json(char *sid, char *obuf)
{
char *json;
json = malloc(strlen(obuf)+37);
strcpy(json, "{\"sessionline\":{\"sid\":\"");
strcat(json, sid);
strcat(json, "\",\"line\":\"");
strcat(json, obuf);
strcat(json, "\"}}");
return json;
}
int main()
{
char *sid = "xyzxyzxyz";
char *obuf = "asdfasdfasdfasdf";
char *json = get_json(sid, obuf);
printf(json);
}
您将返回一个字符*作为字符 您的函数声明应该如下所示:
char* get_json(char *sid, char *obuf)
您似乎还在json
中添加了超过给定大小的内容
SEGFULT发生的原因是,在从char*
到char
的转换过程中,数字被截断为1B,因此printf(json)
尝试读取0-255之间的某个字节的内存,而它以前没有保留该字节。get_json
应该返回指针char*
,而不是char
sid
,因此您的程序将导致超出范围的访问并调用未定义的行为printf()的格式字符串中是危险的
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char get_json(char *sid, char *obuf)
{
char *json;
json = malloc(strlen(sid)+strlen(obuf)+37);
if(json == NULL) return json;
strcpy(json, "{\"sessionline\":{\"sid\":\"");
strcat(json, sid);
strcat(json, "\",\"line\":\"");
strcat(json, obuf);
strcat(json, "\"}}");
return json;
}
int main(void)
{
char *sid = "xyzxyzxyz";
char *obuf = "asdfasdfasdfasdf";
char *json = get_json(sid, obuf);
if (json != NULL) fputs(json, stdout);
}
#包括
#包括
#包括
char get_json(char*sid,char*obuf)
{
char*json;
json=malloc(strlen(sid)+strlen(obuf)+37);
if(json==NULL)返回json;
strcpy(json,“{\”sessionline\”:{\“sid\”:\”);
strcat(json,sid);
strcat(json,“\”,“line\”:“);
strcat(json,obuf);
strcat(json,“\”}”);
返回json;
}
内部主(空)
{
char*sid=“xyzxyzxyz”;
char*obuf=“asdfasdfasdfasdf”;
char*json=get_json(sid,obuf);
if(json!=NULL)fputs(json,stdout);
}
或者更简单地说:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char get_json(char *sid, char *obuf)
{
char *json;
json = malloc(strlen(sid)+strlen(obuf)+37);
if(json == NULL) return json;
sprintf(json, "{\"sessionline\":{\"sid\":\""
"%s"
"\",\"line\":\""
"%s"
"\"}}", sid, obuf);
return json;
}
int main(void)
{
char *sid = "xyzxyzxyz";
char *obuf = "asdfasdfasdfasdf";
char *json = get_json(sid, obuf);
if(json != NULL) fputs(json, stdout);
}
#包括
#包括
#包括
char get_json(char*sid,char*obuf)
{
char*json;
json=malloc(strlen(sid)+strlen(obuf)+37);
if(json==NULL)返回json;
sprintf(json,“{\'sessionOnline\”:{\'sid\”:\“
%s
“\”,“\”行“:\”
%s
“\”}”,sid,obuf);
返回json;
}
内部主(空)
{
char*sid=“xyzxyzxyz”;
char*obuf=“asdfasdfasdfasdf”;
char*json=get_json(sid,obuf);
if(json!=NULL)fputs(json,stdout);
}
您忘记了函数返回类型中的*
。您可以使用sprintf()简化所有这些函数,它看起来像一个打字错误char-get_-json(char*sid,char*obuf)
应该是char*get_-json(char*sid,char*obuf)
。请注意返回类型上的*
。我无法计算字符串文本的长度,但您确实记得分配足够的内存来容纳字符串终止符吗?而且你还应该释放你分配给某处的内存。谢谢纳萨诺利弗,这非常有效!打字错误问题应该关闭。您应该投票/标记关闭而不是应答。这行不是同时为json变量分配*sid和*obuf长度吗?json=malloc(strlen(sid)+strlen(obuf)+37);37是静态json-sid的其余部分,obuf将是动态大小。@是的,但在您的问题中,您使用了行malloc(strlen(obuf)+37)
感谢您提供的其他提示。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char get_json(char *sid, char *obuf)
{
char *json;
json = malloc(strlen(sid)+strlen(obuf)+37);
if(json == NULL) return json;
sprintf(json, "{\"sessionline\":{\"sid\":\""
"%s"
"\",\"line\":\""
"%s"
"\"}}", sid, obuf);
return json;
}
int main(void)
{
char *sid = "xyzxyzxyz";
char *obuf = "asdfasdfasdfasdf";
char *json = get_json(sid, obuf);
if(json != NULL) fputs(json, stdout);
}