c ++中的指针(const指针const变量) [英] pointers in c++ (const pointers const variables )
问题描述
void foo1( char* const p )
{
p = "A";
*p = 'A';
}
void foo2( const char* p )
{
p = "A";
*p = 'A';
}
void foo3( const char* const p )
{
p = "A";
*p = 'A';
}
这些函数中有错误,我可以编译它们并找出它们但我无法想象为什么会出现错误......我想我需要一些帮助...有人会告诉我为什么吗?
there are errors in these functions and i could compile them and figure them out but i couldn''t figure out why are there errors ... i guess i need some help... would anyone tell me why?
推荐答案
你应该注意编译器的消息。 />
You should pay attention at compiler messages.
1 void foo1( char* const p )
2 {
3 p = "A";
4 *p = 'A';
5 }
6 void foo2( const char* p )
7 {
8 p = "A";
9 *p = 'A';
10 }
11 void foo3( const char* const p )
12 {
13 p = "A";
14 *p = 'A';
15 }
gcc version 4.4.5(with -Wall 开关)产生以下输出:
gcc version 4.4.5 (with -Wall switch) produces the following output:
foo.cpp:3:错误:分配只读参数'p'
foo.cpp:3:警告:不推荐将字符串常量转换为'char *'
foo.cpp:在函数'void foo2(const char *)'中:
foo.cpp:9:错误:分配只读位置'* p'
foo.cpp:在函数'void foo3(const char *)'中:
foo.cpp:13:错误:分配只读参数'p'
foo.cpp:14:错误:分配只读位置'*(const char * )p'
- 第一个错误消息,即只读参数p的分配只是说明你不能改变常量的值(因为
p
是常数:它必须始终指向相同的地址。)
(警告:不推荐将字符串常量转换为'char *'表示字符串文字不应分配给字符指针(因为字符串文字不能改变)。 - 第9行的错误,即:只读位置'* p'的分配告诉你不能改变一个常量字符串的字符( p 不是常数但它指向一个常量字符串)。
- 第13,14行的错误告诉你不能既不指定常量指针也不指定常量指向缓冲区。
- The first error message, namely "assignment of read-only parameter ‘p’" simply states you cannot change the value of a constant (because
p
is a constant: it must always point to the same address).
(The warning: "deprecated conversion from string constant to ‘char*’" states that a string literal should not be assigned to a char pointer (because string literals cannot be changed). - The error of line 9, namely: "assignment of read-only location ‘* p’" tells you cannot change a character of a constant string (p is not constant but it points to a constant string).
- Errors of lines 13, 14 tells that you cannot assign neither a constant pointer not a constant pointed buffer.
对于变量(或函数参数)声明,如果你从右到左阅读它会有帮助,i。即从变量名开始:
For variable (or function argument) declarations, it helps if you read them right to left, i. e. starting at the name of the variable:
char* const p
表示 p
是 const
和点('' *
'')到 char
类型的对象。关于你的功能代码,相关的信息是p是const(即不能改变),它指向的char不是(即它可以改变)。
同样;
means p
is const
and points (''*
'') to an object of type char
. With respect to your function code, the relevant information is that p is const (i. e. cannot be changed) and the char it points to is not (i. e. it can be changed).
Similarly;
const char* p
表示 p
points(''$ code> * '')类型为 char
的对象,即 const
。相关信息是您可以更改 p
,因为它不是 const
,但您无法更改它指向的字符。
另请查看这个不错的网站以自动翻译C / C ++将声明类型转换为可读的英语。 ; - )
means that p
points (''*
'') to an object of type char
that is const
. The relevant information is that you can change p
as it is not const
, but you cannot change the character it points to.
Also check out this nice site for automatically translating C/C++ type declarations into readable english. ;-)
我认为你不会对const指针和指向const的指针感到困扰。这个话题有点令人困惑,但它非常重要。我见过你的问题
我坚信这是你的功课。好的,他们中的许多人对这个话题都有疑问。我想在问这种类型的错误之前,你可以在谷歌搜索它。
我可以讨论关于这个话题的一个很好的讨论点。您可以从以下链接中阅读。
1. http://stackoverflow.com/questions/6851436/difference-between-const-char-char-const-const-char-const-string-
2. http://www.go4expert.com/forums/showthread.php?t=26816
它对你有用。
谢谢
I think you are not bothered about const pointer and pointer to const. This topic is little bit confusing but it is very important. I have seen your question
I strongly believe this is your homework. Okay, Many of them have doubt about this topic. I think before asking this type of errors, you can search it in google.
i can sujjest a good discussion points about this topic. you can read it from the below links.
1. http://stackoverflow.com/questions/6851436/difference-between-const-char-char-const-const-char-const-string-
2. http://www.go4expert.com/forums/showthread.php?t=26816
it will be useful for you.
Thanks
这篇关于c ++中的指针(const指针const变量)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!