I am trying to return a string from a array with 4 hexadecimal values. I have a huge list of hex arrays and need to use it in multiple functions. That's why I want to create a function of comparing the hex arrays and return a string.
int comparehex(byte hex[]){
char * buffer = new char [16];
byte hex1[4] = {0x4C, 0x79, 0x52, 0xA8};
byte hex2[4] = {0xC5, 0x86, 0xA4, 0xB5};
for (byte i = 0; i <=3; i ){
if (hex1[i] != hex[i]){
break;
}
if (i == 3){
return "string";
}
}
return false;
}
The code that I wrote won't even compile:
main.cpp: In function ‘int comparehex()’:
main.cpp:46:14: error: invalid conversion from ‘const char*’ to ‘int’ [-fpermissive]
46 | return "string";
| ^~~~~~~~
| |
| const char*
How can I return a string?
CodePudding user response:
Function Declaration general rule:
return_type function_name( parameter list );
in your case:
int comparehex(byte hex[])
return_type : int function_name : comparehex parameters : byte array.
As per declaration, function is supposed to return int. but as per your code
return "string"; // returns a STRING
}
}
return false; // return a boolean
To return a string output, declare the return_type as std::string as in. to do that you need to include library.
#include <string>
std::string comparehex(byte hex[])
Although the return of boolean is typecasted to int (implicit type conversion).it is not a good practice and is considered unsafe. refer to Implicit type conversion in C for more details.
CodePudding user response:
Here is an example of returning char*
with nullptr
as a special type of "sentinel value" to mean "false":
There are a lot of potential things to address here, and we don't really understand your purpose of the function or use-case, but let me just address your immediate code and one viable solution.
There are many potential ways to handle this. Again, here is just one. But, I have to make some assumptions to even answer. Let's assume that:
- Your function needs to return either a string literal OR
false
(or something equivalent to represent this).- This means you will NOT be generating a string inside the function. You will ONLY return a string literal. Otherwise, my example may not meet your need.
- You will never need to return
true
. Rather, the existence of a string indicatestrue
as well.
In this case:
// 1. Change this:
int comparehex(byte hex[])
// to this:
const char* comparehex(byte hex[])
// 2. change this:
return false;
// to this:
return nullptr;
// now this line is fine too:
return "string";
Now, the function returns either nullptr
to indicate false
, OR a string literal such as "string"
.
You'd simply check to see if "false" was intended by checking like this:
char* str = comparehex(some_hex_array);
if (str == nullptr)
{
// `comparehex()` essentially returned `false`, so do what you need to do here
}
else
{
// str was set to some string, so use its value (ex: print it)
printf("%s\n", str);
}
Final notes:
- Again, if you're generating a new string inside the function at run-time, rather than returning a string literal set at compile-time, the above 2 changes are not sufficient.
- Also note that the above code is rather "C-like". It is perfectly valid C , but only one of many ways to handle the above scenario.
- And lastly,
nullptr
here can be considered a type of "sentinel value", which means simply that it is a special value of your return type (char*
) to indicate a special meaning:false
in this case. And therefore, by extension, this sentinel value ofnullptr
also possesses the special meaning of whatever you intend "false" to mean.
CodePudding user response:
In your case, you could add another parameter for passing by reference:
int comparehex(byte hex[], std::string& result_string);
In your code, before returning, set the result_string
parameter to the string you want to return.