const char *,char const *,const char const *&字符串存储 [英] Difference between const char*, char const*, const char const* & string storage

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

问题描述

首先,有什么区别:

(1) const char*
(2) char const*
(3) const char const*

完全,但我想有人给我一个句子,具体来说,所以它坚持在我的头。这是我很好的事情之一,直到有人把我放在现场,然后它变得模糊!

I'm fairly certain I understand this fully, but I'd like someone to give me a sentence for each, specifically, so it sticks in my head. It's one of those things that I'm fine with until someone puts me on the spot and then it gets fuzzy!

此外,如何string-literalls由编译器存储?

Also, how are string-literalls stored by the compiler? This isn't homework, I'm just brushing up on C for interviews in case anyone cares.

推荐答案

1和2分别是等效,并指定指向const char 的指针的类型。指针本身不是const。 3无效,因为它重复const。这就像说 const const int 。顺序是不相关的,所以它也就像说 int const int

1 and 2 are equivalent, and specify the type of a pointer to a const char. The pointer itself is not const. 3 is invalid, because it repeats "const". It's like saying const const int. The order is not relevant, so it's also like saying int const int.

在C99中,有效的是重复 const 。但在C ++中,你不能重复它。

In C99 it is valid to repeat const like that. But in C++, you cannot repeat it.


同样,编译器如何存储字符串literalls?

Also, how are string-literalls stored by the compiler?

它们以未指定的方式存储。但编译器允许将它们存储在程序的只读部分。所以你不能写字符串文字。您可以确保它们在整个程序生命周期内保持分配状态(换句话说,它们具有静态存储持续时间)。

They are stored in an unspecified way. But compilers are allowed to store them in a read-only portion of the program. So you cannot write to string literals. You are guaranteed that they stay allocated through the whole program lifetime (in other words, they have static storage duration).


这不是家庭作业,我只是在C上进行面试,以防任何人关注。

This isn't homework, I'm just brushing up on C for interviews in case anyone cares.

你应该知道C和C ++之间的细微差别。在C99中,如上所述,允许 const const int 。在C89和C ++它被禁止。然而在C ++中,如果应用于本身为const的typedef,可以引入一个冗余的const:

You should be aware of the subtle differences between C and C++. In C99, like explained above, const const int is allowed. In C89 and C++ it is forbidden. In C++ you can however introduce a redundant const, if applied to a typedef that is itself const:

typedef int const cint;
cint const a = 0; // this const is redundant!

同样适用于模板参数。

Same goes for template parameters.

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

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