1. 首先确定一点,int const i;与const int i;是一样的,都是定义一个只读的int i。
2. 所以int const *p;与const int *p;也是一样的,都是定义一个只读的int *p。
但是,不管是int const *p;还是const int *p;,这里有几点需要注意。
#include <stdio.h>
int main()
{
int i1 = 30;
int i2 = 40;
const int *p = &i1;
p = &i2; // no problem
i2 = 80;
printf("%d\n", *p); // output: 80
// *p = 100; // error: assignment of read-only location
return 0;
}
首先是*p只读,并不是p只读,所以p的值是可以改的(p = &i2;);第二,&i1应该只是一个int *,所以把一个int *赋值给const int *是可以的(const int *p = &i1;);第三,p = &i2;之后,对&i2这块地址的访问就有两种方式,一是非const的i2,二是const的*p,所以可以有i2 = 80;,而不能有*p = 100;
3. int * const p;是定义了一个只读的p,所以假如有int * const p = &i1;之后,就不能再有p = &i2;了。但是*p的值是可以随便改的。
4. 把一个const int *赋值给int *也是可以的(MinGW下)(int *p3 = &ci)
#include <stdio.h>
int main()
{
const int ci = 200;
int *p3 = &ci;
*p3 = 250;
printf("%d\n", *p3); // output: 250
return 0;
}
这样其实是可以去修改一个const int的……
5. const int * const p;就是说p和*p都是只读的,结合2、3即可得它的特性。
分享到:
相关推荐
ldr sp, DW_STACK_START @ setup stack pointer mov fp, #0 @ no previous frame, so fp=0 @ copy U-Boot to RAM ldr r0, =TEXT_BASE mov r1, #0x0 mov r2, #0x30000 bl nand_read_ll tst r0, #0x0 beq ok_...
CRect operator+(const RECT* lpRect) const; CRect operator-(const RECT* lpRect) const; }; ///////////////////////////////////////////////////////////////////////////// // CPoint - A 2-D point, ...
#define _LIBICONV_VERSION 0x010E /* version number: (major) + minor */ extern @DLL_VARIABLE@ int _libiconv_version; /* Likewise */ /* We would like to #include any system header file which could ...
// pointer to the interface used for drawing SIZE m_frameSize; SIZE m_frameOffset; UINT m_nDelay; // delay (in 1/100s of a second) UINT m_nDisposal; // disposal method }; #pragma pack(1) // turn...
Friends Exceptions Run-Time Type Information (RTTI) Casting Streams Preincrement and Predecrement Use of const Integer Types 64-bit Portability Preprocessor Macros 0 and NULL sizeof Boost C++0x ...
const N int = int(unsafe.Sizeof(0)) func main() { x := 0x1234 p := unsafe.Pointer(&x) p2 := (*[N]byte)(p) if p2[0] == 0 { fmt.Println(“本机器:大端”) }
Const C1= 17856; C2= 23589; type TForm1 = class(TForm) Image1: TImage; Edit1: TEdit; Label1: TLabel; Label2: TLabel; Edit2: TEdit; Button1: TButton; procedure FormCreate(Sender: ...
1.20 const char *p、char const *p和char *const p有什么区别? 10 复杂的声明 11 1.21 怎样建立和理解非常复杂的声明?例如定义一个包含N个指向返回指向字符的指针的函数的指针的数组? 11 1.22 如何声明...
Changes in 2.4.6 (February 22, 2011): Brief summary : - Support more host OS to run on: - Include win64 native binary in the release. - Fixed failures on big endian hosts. - BIOS: Support for up to...