avatar
a simple C++ question# Programming - 葵花宝典
s*y
1
actually three:
1. what's the difference of
const char* p
char const *p
char* const p
what i understood is---first is a pointer to constant object;
second and third are same , a constant pointer to an object
am i right?
2. is this true:
const char* p = const char *p
3. why const char* can be both a character and a string declaration
avatar
t*l
2

first and second are the same, pointer to constant object
yes
how?

【在 s****y 的大作中提到】
: actually three:
: 1. what's the difference of
: const char* p
: char const *p
: char* const p
: what i understood is---first is a pointer to constant object;
: second and third are same , a constant pointer to an object
: am i right?
: 2. is this true:
: const char* p = const char *p

avatar
z*i
3
A small piece of code to verify your guess. :)
/*
By ZuZi Sun Nov 4 19:16:45 CST 2007
* const int* p: a int* pointer pointing to a const object
* int* const p: a char* pointer who is a const type itself
* int const *p: same with const char* p
*/
#include
using namespace std;
int main()
{
int a = 1;
int b = 10;
// an int* ponter pointing to a const int
const int* p2const = &a;
//(*p2const)++; //error: increment of read-only location
p

【在 s****y 的大作中提到】
: actually three:
: 1. what's the difference of
: const char* p
: char const *p
: char* const p
: what i understood is---first is a pointer to constant object;
: second and third are same , a constant pointer to an object
: am i right?
: 2. is this true:
: const char* p = const char *p

avatar
s*y
4
thanks for the answers, to summarize,
const char *p = const char* p = char const *p = char const* p
a pointer to constant object
char* const p
a constant pointer to an object
is this right?

【在 z**i 的大作中提到】
: A small piece of code to verify your guess. :)
: /*
: By ZuZi Sun Nov 4 19:16:45 CST 2007
: * const int* p: a int* pointer pointing to a const object
: * int* const p: a char* pointer who is a const type itself
: * int const *p: same with const char* p
: */
: #include
: using namespace std;
: int main()

avatar
r*y
5
right
parse at the asterisk

【在 s****y 的大作中提到】
: thanks for the answers, to summarize,
: const char *p = const char* p = char const *p = char const* p
: a pointer to constant object
: char* const p
: a constant pointer to an object
: is this right?

avatar
c*x
6
Key word Const control the attribute of the variable of its right
hand side, keep this in mind, it can't fool you.
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。