为const char * const的对比为const char *? [英] const char * const versus const char *?

查看:86
本文介绍了为const char * const的对比为const char *?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过一些示例程序运行与C ++来refamiliarize我自己和我遇到了以下问题。首先,这里是例如code:

I'm running through some example programs to refamiliarize myself with C++ and I have run into the following question. First, here is the example code:

void print_string(const char * the_string)
{
    cout << the_string << endl;
}

int main () {
    print_string("What's up?");
}

在上面的code,参数为print_string本来代替了为const char * const的the_string 。这将是这更正确的?

In the above code, the parameter to print_string could have instead been const char * const the_string. Which would be more correct for this?

据我所知,所不同的是,一个是指向一个恒定字符,而一个是恒定指针为恒定的字符。但为什么这两个的工作?什么时候会是相关的?

I understand that the difference is that one is a pointer to a constant character, while one is a constant pointer to a constant character. But why do both of these work? When would it be relevant?

推荐答案

后者prevents你修改 the_string print_string 。它实际上是合适的在这里,但也许是冗长推迟开发。

The latter prevents you from modifying the_string inside print_string. It would actually be appropriate here, but perhaps the verbosity put off the developer.

的char * the_string :我可以改变字符 the_string 点,我可以修改字符在它指向。

char* the_string : I can change the char to which the_string points, and I can modify the char at which it points.

为const char * the_string :我可以改变字符 the_string 点,但我不能修改字符在它指向。

const char* the_string : I can change the char to which the_string points, but I cannot modify the char at which it points.

char * const的the_string :我不能更改字符 the_string 点,但我可以修改字符在它指向。

char* const the_string : I cannot change the char to which the_string points, but I can modify the char at which it points.

为const char * const的the_string :我不能更改字符 the_string 点,我也可以修改字符在它指向。

const char* const the_string : I cannot change the char to which the_string points, nor can I modify the char at which it points.

这篇关于为const char * const的对比为const char *?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆