我正在讨论C并且对const使用指针有疑问.我理解以下代码:
const char *someArray
Run Code Online (Sandbox Code Playgroud)
这是定义指向char类型的指针,const修饰符意味着someArray不能更改存储的值.但是,以下是什么意思?
char * const array
Run Code Online (Sandbox Code Playgroud)
这是另一种指定参数的替代方法,该参数是指向名为"array" const且不能修改的数组的char指针吗?
最后,这个组合意味着什么:
const char * const s2
Run Code Online (Sandbox Code Playgroud)
作为参考,这些来自第7章中的Deitel C编程书,所有这些都用作传递给函数的参数.
sep*_*p2k 47
const char* 就像你说的那样,是一个指向char的指针,你不能改变char的值(至少不通过指针(没有强制转换const)).
char* const 是指向char的指针,您可以在其中更改char,但不能使指针指向其他char.
const char* const 是一个常量char的常量指针,即你既不能改变指针指向的位置也不能改变指针的值.
mlo*_*kot 17
从什么是const int的*之间的差异,const int的*const的,INT常量*:
向后读它......
Run Code Online (Sandbox Code Playgroud)int* - pointer to int int const * - pointer to const int int * const - const pointer to int int const * const - const pointer to const int
Kaz*_*oom 13
//pointer to a const
void f1()
{
int i = 100;
const int* pi = &i;
//*pi = 200; <- won't compile
pi++;
}
//const pointer
void f2()
{
int i = 100;
int* const pi = &i;
*pi = 200;
//pi++; <- won't compile
}
//const pointer to a const
void f3()
{
int i = 100;
const int* const pi = &i;
//*pi = 200; <- won't compile
//pi++; <- won't compile
Run Code Online (Sandbox Code Playgroud)
}
你应该试试cdecl:
~ $ cdecl Type `help' or `?' for help cdecl> explain const char *someArray declare someArray as pointer to const char cdecl> explain char * const someArray declare someArray as const pointer to char cdecl> explain const char * const s2 declare s2 as const pointer to const char cdecl>