Pointer Who are there? 0.0
Basic Description:
c ++ is an underlayer in contact with more language, and nothing more than the underlying memory, each memory address corresponds. The pointer is a new type, just like int he represents signed integer, but said he was a string of hexadecimal address. In x86, in order to fully address pointer. So it is defined as 4 bytes, and in x64-, 8 bytes!
Basic elements:
1, an address operation needs to
2, the memory capacity to be operated
Leads syntax:
Name * pointer data type;
for example: int * pstr;
The definition of confusion:
int* pstr=0, str=0;
int* pstr1 = 0, *str1 = 0;
Contrast two lines: I believe that many novice programmers may have found the problem is not one, but we look at the difference between the first and second rows, and the second variable is whether there is an asterisk. What is actually going to explain it?
code show as below:
cout << typeid(pstr).name() <<" “<< typeid(str).name() << endl;
cout << typeid(pstr1).name()<<” "<< typeid(str1).name() << endl;
result:
------------- caring people believe that the differences have been found, although a small problem! But it is worth noticing ----------------- !!!
Confuse the concept? does not exist:
Ⅰ: an array of pointers and pointer array
1, array of pointers: the nature of the array is, for example: int type pointer array; it is just the same as an array of type int, short type array that is an array, but each member is int * type.
2, an array of pointers: essentially a pointer, he and an int pointer, short-hair pointer What difference does it make? He was referring to something just a little big, he points to an array.
Code Example (one to understand):
int a{ 1 }, b{ 2 }, c{ 3 }; //定义三个变量
int* pArr[3]{}; //定义指针数组
pArr[0] = &a; pArr[1] = &b; pArr[2] = &c;//每个成员都是指针而已
int aa[3] = { 1,2,3 }; //定义一个大小3的数组
int(*Arrp)[3]{};//定义数组指针
Arrp = &aa; //它指向一个大小为3的数组
Ⅱ constant pointer, pointer constant constant constant pointer pointing
1, constant pointer: essentially a pointer! Variable pointer corresponds (own) is generated, for example: int pointer matter, short pointer friends. What is it? Pointer constant, what is constant is? const int, const short of it; constant change? The answer is no, it is not by variable * = xxx to change the size constant, it doing it? It can point to other variables constant even
2, pointer constants: nature is constant! There are int constants, short constants; it is a pointer type constants only. For example: int x is a constant pointer to type int * const x; it is what features it? It can point to a number, but can not change the point; but it can be changed by the value of a variable number *
** 3, const pointer constant: ** satisfying the above two features is this
Code Example:
1, constant pointer
Code:
const int str1 = 10; const int str2 = 20; int str3 = 30;
//定义常量指针
const int* cpstr1{ &str1 }; //*cpstr1 = 5; 都不能修改值
cout << "cpstr1 = " << *cpstr1 << endl;
cpstr1 = &str2; // *cpstr1 = 5; 都不能修改值
cout << "cpstr1 = " << *cpstr1 << endl;
cpstr1 = &str3; // *cpstr1 = 5; 都不能修改值
cout << "cpstr3 = " << *cpstr1 << endl;
Results:
Summary: a constant pointer may point to other variables or constants, but can not be changed by an asterisk points to the address value
2, pointer constant
Code:
int str1{ 10 }, str2{ 20 };
int* const pcstr = &str1;
cout << "修改前 str1=" << *pcstr << endl;
//pcstr = &str2; 这句话不可能成功,因为指向了就不能改变了
*pcstr = 30;
cout << "修改后 str1=" << str1 << endl;
Results:
Summary: pointer can point to a first constant number, can not be changed after the point; variable to change the value pointed by asterisk
3, constant const pointer
Code:
const int str1{ 10 }; const int str2{ 20 };
const int* const cpcstr = &str1;
cout << "str1=" << *cpcstr << endl;
//*cpcstr = 30; //这句话不可能成功
//cpcstr = &str2;//这句话也不可能成功
Results:
Summary: After the point, can not change the point, can not be modified by an asterisk pointing to the value of the address, in short, is read-only
Thanks to their hard work! ! Old iron who feel that there is some truth, remember that the point of a like oh! ! ! Thanks!