If i have a function api that expects a 14 digit input and returns a 6 digit output. I basically define the input as a const char *. would that be the correct and safe thing to do? also why would I not want to just do char * which I could but it seems more prudent to use const char * in that case especially since its an api that i am providing. so for different input values I generate 6 digit codes.
7 Answers
I am not sure why are you using char pointers, where you could use std::string:
std::string code(const std::string& input) { ... } If you don't have the choice, using const char* gives a guarantee to the user that you won't change his data especially if it was a string literal where modifying one is undefined behavior.
Comments
By using const you're promising your user that you won't change the string being passed in. It becomes part of the API helping define your function's behavior. It also let's users pass constant strings, including literal strings like "mystring".
1 Comment
const char* indicates that string literals can be passed to a function - before I began using const in my C code this was always a bane to remember if I could or could not.When you say const char *c you are telling the compiler that you will not be making any changes to the data that c points to. So this is a good practice if you will not be directly modifying your input data.
1 Comment
String literals have static storage class (they exist for the duration of the program) and may or may not be shared if the same string literal is referenced from multiple locations in a program. The effect of modifying a string literal is undefined; thus, you should always declare a pointer to a string literal as const char *.
Comments
You get several benefits for using const:
- It documents your code, the user knows no harm will be done to this string.
- You allow the user to send a
const char*which he might have. Converting from non-consttoconstis automatic. The other way around is something that should be avoided (And done explicitly, and might lead to undefined behavior at times) - You let the compiler check you. The compiler can now verify that you don't accidentally change the user's string.
Comments
You need to use a const char * anywhere that you're passing a string literal, or the compiler will balk (assuming you don't want to convert it to a std::string).
1 Comment
char*, but it is not a good idea to do so.const char* is usually used in parameters, stating that your function won't modify that string.
void function(char* modified_str, const char* not_modified_str) { ... } If you're returning the const char* what you want to say is not obvious. You try to tell that nobody should modify the returned string, but you still (I think it would be that way) transfer the ownership to the calling routine, so that it would have to invoke delete[] on the char that your function returned.
Generally speaking, use std::string, then your function will look the following way:
std::string function(std::string& modified_str, const std::string& not_modified_str) { ... }