如何从函数返回一个字符串?这种函数中的返回类型应该是什么 [英] How to return a string from a function? What should be the return type in such function
问题描述
我已经写了一个返回字符串的prgram。我的返回类型是char ??
我没有得到progaram的输出。在那里任何一个可以帮助我做这件事的人...如果它返回字符串,应该是该程序的返回类型
什么我试过了:
i试图返回类型为char的字符串...但我知道它是错误的。这是正确的类型program?
常用方法是使用malloc()
为字符串分配内存并返回char *
指向该分配内存的指针。然后调用函数负责释放内存:
char * SomeFunc()
{
// 确定所需的大小并分配
// 不要忘记为终止NULL char添加一个
char * buf =( char *)malloc(requiredSize);
// 写入buf,确保不超过requiredSize
// 调用函数必须释放内存
返回 buf;
}
在某些情况下,您还可以使用static char []
缓冲区。然后将const char *
返回到该缓冲区。但你应该知道这些函数可能存在的问题(固定大小,而不是线程安全)。
const char * SomeFunc()
{
static char buf [SOME_FUNC_STATIC_BUF_SIZE] = ;
// 写入buf,确保不超过SOME_FUNC_STATIC_BUF_SIZE
return buf;
}
另一种解决方案是将缓冲区及其大小作为参数传递:
char * SomeFunc( char * buf, size_t 大小)
{
// 写入buf,确保不超过
return buf;
}
使用C ++,使用类似std :: string
的C ++字符串类型,然后返回。然后你不必关心释放内存:
std :: string SomeFunc()
{
std :: string buf;
// 分配到此处的buf
返回 buf;
}
最好返回一些字符串对象,如 STD:字符串。
另一个解决方案是返回一个char *但它有一些问题,比如
a)你分配的字符串需要在其他地方释放或你的
b)从功能范围到缓冲区,需要制作副本。char * getStringA()
{
char * p = new char [ 12 ]。
strcyp(p, hello world);
return p;
}
char * getStringB()
{
char p [ 12 ];
strcyp(p, hello world);
return p;
}
// 致电A
char * resultA = getStringA();
// 用法
/ / cleanup
delete resultA;
// 致电B
char * resultB = getStringB();
char buffer [ 12 ]; // 必须足够大
strcyp(缓冲区,resultB(
// / span> 用法
// 无需清理! !! 跨度>
i have written a prgram that return a string.my return type was char??
and i get no output for the progaram .is there any one who can help me do this...what should be the retuen type of the program if it return string of character
What I have tried:
i have tried to return a string with type char...but i know it is wrong.what is the correct type for the program ??
The common method is to allocate memory for the string usingmalloc()
and returning achar*
pointer to that allocated memory. The calling function is then responsible for freeing the memory:
char *SomeFunc() { // Determine required size and allocate // Don't forget to add one for the terminating NULL char char *buf = (char *)malloc(requiredSize); // Write to buf here ensuring that requiredSize is not exceeded // Calling function must free the memory return buf; }
In some cases you can also use astatic char[]
buffer. Then return aconst char*
to that buffer. But you should know about the possible problems witch such functions (fixed size, not thread safe).
const char *SomeFunc() { static char buf[SOME_FUNC_STATIC_BUF_SIZE] = ""; // Write to buf here ensuring that SOME_FUNC_STATIC_BUF_SIZE is not exceeded return buf; }
Another solution is passing the buffer and its size as arguments:
char *SomeFunc(char *buf, size_t size) { // Write to buf here ensuring that size is not exceeded return buf; }
With C++, use a C++ string type likestd::string
instead and return that. Then you don't have to care about freeing memory:
std::string SomeFunc() { std::string buf; // Assign to buf here return buf; }
At best you return some string object like std:string.
Another solution is to return a char* but it has some problems like
a) the string you allocated needs to be freed elsewhere or your
b) are poiting to a buffer from the function scope and need to make a copy.char *getStringA() { char * p = new char[12]; strcyp( p, "hello world"); return p; } char *getStringB() { char p[12]; strcyp( p, "hello world"); return p; } //call A char *resultA = getStringA(); // usage //cleanup delete resultA; //call B char *resultB = getStringB(); char buffer[12];// must be big enough strcyp( buffer, resultB( // usage // no cleanup!!!
这篇关于如何从函数返回一个字符串?这种函数中的返回类型应该是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!