在C中返回字符串的正确方法 [英] Proper way to return a string in C
本文介绍了在C中返回字符串的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
char* get_address_string(PACKAGE* pkg){
char *c;
sprintf(c, "%02x:%02x:%02x:%02x:%02x:%02x", pkg->address[0], pkg->address[1],
pkg->address[2], pkg->address[3], pkg->address[4], pkg->address[5]);
return c;
}
代码工作正常.但是,我知道这不是在C中返回字符串的正确方法.我收到警告在此函数中未初始化使用c".
The code works fine. However, I know this is not the proper way to return a string in C. I am receiving the warning "c is used uninitialized in this function".
用C编写此函数的正确方法是什么?
What is the proper way to write this function in C?
推荐答案
c
是指针,但未分配内存.返回值确定,这就是在C语言中可以完成的操作.
c
is a pointer, but no memory is allocated. The return value is ok, that's how it can be done in C.
但是您需要分配内存.
这篇关于在C中返回字符串的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文