开发者

when to use const char *

开发者 https://www.devze.com 2022-12-29 05:47 出处:网络
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?

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.


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.


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


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.


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


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.


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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号