简体   繁体   中英

when to use const char *

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.

您需要在传递字符串文字的任何地方使用const char * ,否则编译器会犹豫不决(假设您不想将其转换为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) { ... }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM