C语言 char* vs const char* 作为参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3179645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
char* vs const char* as a parameter
提问by Sunscreen
There are many times that I get compile errors when I use char*instead of const char*. So, I am not sure the actual difference, the syntax and the compile mechanism.
当我使用char*而不是const char*. 所以,我不确定实际的区别、语法和编译机制。
回答by Rob Wells
If you're after the difference between the two, just think of them as:
如果您追求两者之间的差异,只需将它们视为:
- char*is a pointer that points to a location containing a value of type char that can also be changed. The pointer's value can be changed, i.e. the pointer can be modified to point to different locations.
- const char*is a pointer, whose value can be also changed, that points to a location containing a value of type char that cannotbe changed.
- char*是一个指针,指向一个包含 char 类型值的位置,该值也可以更改。指针的值可以改变,即指针可以修改为指向不同的位置。
- const char*是一个指针,它的值也可以改变,它指向一个包含不能改变的char 类型值的位置。
回答by T.J. Crowder
const char *means "pointer to an unmodifiable character." It's usually used for strings of characters that shouldn't be modified.
const char *表示“指向不可修改字符的指针”。它通常用于不应修改的字符串。
Say you're writing this function:
假设你正在编写这个函数:
int checkForMatch(const char * pstr)
You've promised (through the function signature) that you will not changethe thing pointed to by pstr. Now say part of checking for a match would involve ignore the case of letters, and you tried to do it by converting the string to upper case before doing your other checks:
你已经答应(通过函数签名),你将不会改变指向的东西pstr。现在说检查匹配的一部分将涉及忽略字母的大小写,并且您尝试通过在执行其他检查之前将字符串转换为大写来做到这一点:
strupr(pstr);
You'll get an error saying you can't do that, because strupris declared as:
你会得到一个错误,说你不能这样做,因为strupr被声明为:
char * strupr(char* str);
...and that means it wants to be able to write to the string. You can't write to the characters in a const char *(that's what the constis for).
...这意味着它希望能够写入字符串。您不能写入 a 中的字符const char *(这就是 theconst的用途)。
In general, you can pass a char *into something that expects a const char *without an explicit cast because that's a safe thing to do (give something modifiable to something that doesn't intend to modify it), but you can'tpass a const char *into something expecting a char *(without an explicit cast) because that's not a safe thing to do (passing something that isn't meant to be modified into something that may modify it).
通常,您可以将 a 传递给char *期望 a 的东西,const char *而无需显式转换,因为这是安全的做法(将可修改的东西赋予不打算修改它的东西),但您不能将 a 传递给const char *期望 a 的东西char *(没有显式转换),因为这不是一件安全的事情(将不打算修改的内容传递为可能修改它的内容)。
Of course, this is C, and you can do just about anything in C, including explicitly casting a const char *to a char * — but that would be a really, really bad ideabecause there is (presumably) some reason that the thing being pointed to by the pointer is const.
当然,这是 C,你可以在 C 中做任何事情,包括显式地将 aconst char *转换为char * a——但这将是一个非常非常糟糕的主意,因为(大概)有某种原因导致指针是const。
回答by Yas
- char * :
non-constantpointer tonon-constantcharacter - const char * :
non-constantpointer toconstantcharacter - char *const :
constantpointer tonon-constantcharacter - const char * const:
constantpointer toconstantcharacter
- char * :
non-constant指向non-constant字符的指针 - const char * :
non-constant指向constant字符的指针 - char *const :
constant指向non-constant字符的指针 - const char * const:
constant指向constant字符的指针
参考 [link][关联]
回答by Peter G.
Probably I'm too picky. In my book, the character(s) pointed to by a const char* can possibly be changed but not via the const char*. A const char * can point to modifiable storage. Example:
可能是我太挑剔了。在我的书中,const char* 指向的字符可能会改变,但不能通过 const char* 改变。const char * 可以指向可修改的存储。例子:
char a[] = "abracadabra";
const char * ccp = &a[0]; // ccp points to modifiable storage.
*&a[0] = 'o'; // This writes to a location pointed to by const char* ccp
So, my wording is:
所以,我的措辞是:
A char * is a pointer that be changed and that also allows writing through it when dereferenced via * or [].
char * 是一个可以更改的指针,并且在通过 * 或 [] 取消引用时也允许通过它写入。
A const char * is a pointer that be changed and that does not allow writing through it when dereferenced via * or [].
const char * 是一个可以更改的指针,当通过 * 或 [] 取消引用时不允许写入。
回答by user3405291
I always try to define parameters with const char*not char*because converting from std::stringto conts char*is easy by .c_str()method. However converting std::stringto char*is not that easy.
我总是尝试用const char*not定义参数,char*因为从std::stringto转换conts char*很容易通过.c_str()方法。但是转换std::string到char*可没那么容易。

