问题描述
我要在C和有关于使用指针常量
使用的问题。我了解以下code:
I am going over C and have a question regarding const
usage with pointers. I understand the following code:
const char *someArray
这是定义一个指向类型char和在常量
修改意味着一个指针存储在的someArray
不能改变。然而,什么是下面的意思吗?
This is defining a pointer that points to types of char and the const
modifier means that the values stored in someArray
cannot be changed. However, what does the following mean?
char * const array
这是指定一个参数,该参数是一个字符指针到名为阵的数组是的另一种方式常量
,不能进行修改?
Is this an alternate way of specifying a parameter that is a char pointer to an array named "array" that is const
and cannot be modified?
最后,这是什么意思的组合:
Lastly, what does this combination mean:
const char * const s2
有关的参考,这些是从的Deitel C语言编程的书采取的第7章,所有这些被用作传递给函数的参数。
For reference, these are taken from the Deitel C programming book in Chapter 7 and all of these are used as parameters passed to functions.
推荐答案
为const char *
是,如你所说,一个指向一个char,在那里你可以不改变字符的值(至少不是通过指针(没有铸造常量性远))。
const char*
is, as you said, a pointer to a char, where you can't change the value of the char (at least not through the pointer (without casting the constness away)).
char * const的
是指向一个char,在那里你可以改变字符,但你不能使指针指向一个不同的字符。
char* const
is a pointer to a char, where you can change the char, but you can't make the pointer point to a different char.
为const char * const的
是一个常量指针,以一个恒定的字符,即可以改变既不当指针指向,也不指针对象的值。
const char* const
is a constant pointer to a constant char, i.e. you can change neither where the pointer points nor the value of the pointee.
这篇关于常量的使用与C指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!