const/volatile修饰标识符的进一步论证
const int *p = NULL; 相当于 int const *p = NULL;
下面要写一对与上面一对表达式更清晰的陈述:
const int (*p) = NULL; 相当于 int const (*p) = NULL;
这样其实比起第一对看起来更加清晰。const修饰(*p),即限定p指针所指地址内容的值。
再看下面:
int * const p = NULL;
更清晰地表述为:int (* const p) = NULL;
这样,很明显可以看到const修饰标识符p,即限定p指针的值。
要注意的是没有int (const * p) = NULL;这样的语法。
因为这样一来,对int的修饰会产生冲突。由于const *p对于C/C++而言本身就是一个合法的正则表达式语句。const *p = NULL; 完全OK,这时p作为int const*类型。
利用括号对指针类型进行限定是比较有用的分析技巧。
比如说以下情况:
typedef int ARRAY[10];
void main(void)
{
ARRAY *p; // p是什么类型?
}
那么对于类型定义情况,我们公司很多程序员都搞不清楚。尤其像以上这种情况。
加个括号看看:ARRAY (*p);
分析:由于ARRAY是int[10]类型,将它代入就成为:int[10] (*p);
调整一下:int (*p)[10]; 所以比较自然地得出了p的类型为:int(*)[10]。
指针标记*与标识符靠拢时总是与标识符进行一同规归约。
typedef const int CINT;
CINT *p = NULL; // p是什么类型?
同样地,加括号:CINT (*p) = NULL; 就和const int (*p)一样了。
typedef int* PINT;
PINT const p = NULL; // p是什么类型?
同样地,加括号,不过这里实际上没什么必要了,因为p作为一个单独的标识符没有指针对它进行修饰。const PINT (p) = NULL; 由于PINT为(int*)类型,因此这里的const就是对标识符p作修饰,由于p已经为(int*),因此p的类型为int* const。
若为:PINT const (*p) = NULL;那么p类型为:int* const *。第一个int*是由PINT得到的。
若是:PINT (* const p) = NULL; 那么p类型为int* * const
[解决办法]
學習啊
我一般就記住指針常量
常量指針就行了
多了昏啊
[解决办法]
const int (*p) = NULL; 相当于 int const (*p) = NULL;
楼主啊,你说的都是真的吗?
可是我这里:
/usr/home/jerry/src/cpp/faq$ cat const.c
#include <stdio.h>
const int (*p1) = NULL
int const (*p2) = NULL;
int main(void)
{
int i;
p1 = &i;
p2 = &i;
return (0);
}
/usr/home/jerry/src/cpp/faq$ make const
cc -O -pipe -g -Wall const.c -o const
const.c:4: error: syntax error before "int "
const.c: In function `main ':
const.c:11: error: `p2 ' undeclared (first use in this function)
const.c:11: error: (Each undeclared identifier is reported only once
const.c:11: error: for each function it appears in.)
*** Error code 1
Stop in /usr/home/jerry/src/cpp/faq.
/usr/home/jerry/src/cpp/faq$ cc --version
cc (GCC) 3.4.2 [FreeBSD] 20040728
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
[解决办法]
对标准的理解还是不够啊!
也许是GCC的BUG?
[解决办法]
其实要解释复杂的C和C++的声明,只需要遵循right-left-rule就可以了:
http://www.codeproject.com/cpp/complex_declarations.asp
------解决方案--------------------
谢谢LS
[解决办法]
学习 学习 再学习.
指向常量的指针
常指针
指向常量的常指针
等等这些 以前看了很多
不过lz讨论类型的方法还是比较有意思的.
[解决办法]
const int (*p) = NULL; 相当于 int const (*p) = NULL;
楼主啊,你说的都是真的吗?
不是呀,前面的是说p所指的是一个整型常量,后面的是一个说这个指针是常量的吧!