[英]return value of a bool function and passed pointer as parameter
I am new to C++ having trouble in assigning value to char* of a function. 我是C ++的新手,在为函数的char *赋值时遇到了麻烦。 I have a function as below which returns bool
我有一个如下函数返回布尔值
bool Function(char* inString)
{
int m = strlen(inString);
char output[1001];
memset(output , 0 , sizeof(output));
sprintf_s(output,50,"length is %d",m);
if(m>5)
return true;
if(m<5)
return false;
}
Along with the function , i am trying to get the "output" value on calling this function outside defined local inside this function which has value - "length is -" 与该函数一起,我试图在此函数内部定义的本地外部调用此函数以获取“输出”值,该本地函数具有值-“ length is-”
I tried doing 我试着做
bool Function(char* inString)
{
int m = strlen(inString);
char output[1001];
memset(output , 0 , sizeof(output));
sprintf_s(output,50,"length is %d",m);
sprintf_s(inString,50,output);
if(m>5)
return true;
if(m<5)
return false;
}
But this fails because inString has already a value and this is giving following error Access violation writing location 0x00165267. 但这失败了,因为inString已经有一个值,并且给出了以下错误访问冲突写入位置0x00165267。
Is there any way to get both parameters from this function ( bool value based on string length) as well as b) the string statement "output"? 有什么方法可以从此函数中获取两个参数(基于字符串长度的布尔值)以及b)字符串语句“输出”吗?
I appreciate your help.. 我感谢您的帮助..
How are you calling this function? 您如何调用此函数? If you're calling it with a string literal, then that string literal is likely in read-only memory.
如果使用字符串文字来调用它,则该字符串文字可能位于只读存储器中。 For example, this will fail:
例如,这将失败:
bool result = Function( "longer than 5" );
That will likely result in an access violation because the string "longer than 5"
is likely in read-only memory. 这可能会导致访问冲突,因为字符串
"longer than 5"
可能位于只读存储器中。 (It's not required to be, but it's likely with modern compilers.) (不是必需的,但现代编译器可能会这样。)
Also, as Alexandru pointed out above, you didn't handle the m==5 case at all. 同样,正如Alexandru上面指出的,您根本不处理m == 5的情况。
Edit: If you want to get the string you're generating with sprintf_s
out of the function, you have a couple options. 编辑:如果
sprintf_s
函数中获取使用sprintf_s
生成的字符串,则有两种选择。
char output[1001]
), you could instead have this pointer passed to you. char output[1001]
)外,您还可以将此指针传递给您。 ie. bool Function(char *inString, char *outString);
bool Function(char *inString, char *outString);
static
, and then return a pointer to it by reference. static
,然后通过引用返回指向它的指针。 That requires you to add a second operand. bool Function(char *inString, char **outString);
bool Function(char *inString, char **outString);
: bool Function(char *inString, char **outString);
Then, inside your code you say *outString = output
, which is, frankly, gross. *outString = output
,坦率地说,这是毛额。 One of the advantages of #1 above is that you can use default arguments and an if-statement to make the sprintf
optional: 上面#1的优点之一是您可以使用默认参数和if语句来使
sprintf
可选:
bool Function(char *inString, char *outString = 0)
{
int m = strlen(inString);
if (outString)
sprintf_s(outString, 50, "length is %d", m);
return m >= 5;
}
Also, a stylistic note: Prefer char *inString
to char* inString
. 另外,还有一个风格上的注释:比
char *inString
更喜欢char* inString
。 In C and C++, the asterisk is not part of the type being declared, but rather a modifier on the individual symbol being declared. 在C和C ++中,星号不是声明类型的一部分,而是声明的单个符号上的修饰符。 If you want to declare two character pointers
a
and b
, you write char *a, *b
, not char* a, b
. 如果要声明两个字符指针
a
和b
,请编写char *a, *b
,而不是char* a, b
。
Edit 2: If you believe John Carmack's insistence that you const
anything that could be const
, then the above becomes: 编辑2:如果您认为约翰·卡马克(John Carmack)坚持要常
const
任何可能为const
,那么以上内容将变为:
bool Function(const char *const inString, char *const outString = 0)
{
const int m = strlen(inString);
if (outString)
sprintf_s(outString, 50, "length is %d", m);
return m >= 5;
}
I suppose you could go further and make it return const bool
, except the function's return value is already an rvalue... 我想你可以走得更远,让它返回
const bool
,除了函数的返回值已经是一个右值...
To be able to return a string from the function. 为了能够从函数返回一个字符串。 You need the string to be allocated in such a way that it is alive even after you exit the function.
您需要以一种即使在退出函数后仍然有效的方式来分配字符串。 Your example stores the string in a local array(
output
), which does not live beyond the scope( { }
) of the function. 您的示例将字符串存储在本地array(
output
)中,该数组不会超出函数的作用域( { }
)。
There are several ways to do this: 做这件事有很多种方法:
static
storage static
存储 and so on, which one to use depends on your usage semantics. 依此类推,使用哪种取决于您的用法语义。
You just need to pass the output string as a function parameter. 您只需要将输出字符串作为函数参数传递即可。 Since you want to allocate the string inside the function you need to pass a pointer by reference.
由于要在函数内部分配字符串,因此需要按引用传递指针。
On a side note, consider using std::string
instead of a char *
另外,请考虑使用
std::string
代替char *
Since you are using c++ you should do something like 由于您使用的是c ++,因此您应该执行以下操作
bool Function(std::string &inString, std::string &outString)
{
size_t size = inString.size();
outString = "length is " + std::to_string(size); //if c++11
if (size > 5)
return true;
return false;
}
to convert your size_t to string in C++ you can use boost::lexical_cast
or this define 要将您的size_t转换为C ++中的字符串,可以使用
boost::lexical_cast
或此定义
#include <sstream>
#define SSTR( x ) dynamic_cast< std::ostringstream & >(( std::ostringstream() << std::dec << x ) ).str()
inString = "length is " + SSTR(size);
Your code is not really C++, it's C. 您的代码不是真正的C ++,而是C。
Here's how you should do it in C++. 这是您应该在C ++中执行的方法。 Note the use of references rather than pointers.
注意使用引用而不是指针。
bool Function(const std::string &inString, std::string &outString)
{
int m = inString.size();
std::stringstream ss;
ss << "length is" << m;
outString = ss.str();
return m >= 5;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.