tags:

views:

171

answers:

6

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.

+2  A: 

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.

Justin Ethier
As well as telling the compiler, it's also a helpful hint to the user of the API.
Vicky
+2  A: 

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".

John Gordon
+1 for mentioning that the use of `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.
James Morris
+8  A: 

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.

AraK
+1 I was just writing this myself.
Mark B
A: 

You get several benefits for using const:

  1. It documents your code, the user knows no harm will be done to this string.
  2. You allow the user to send a const char* which he might have. Converting from non-const to const is automatic. The other way around is something that should be avoided (And done explicitly, and might lead to undefined behavior at times)
  3. You let the compiler check you. The compiler can now verify that you don't accidentally change the user's string.
Shiroko
A: 

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).

Ken Bloom
"or the compiler will balk" - depending on warning options. In C++, a string literal can be implicitly converted to a non-const `char*`, but it is not a good idea to do so.
Steve Jessop
A: 

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) { ... }
Kotti