//9 – 1 pointer and address
#include <iostream>
using namespace std;
/*
hex
hexadecimal 十六进制
____________________________________________
0X7694F6A4|0X7694F6A5|0X7694F6A6|…|…| RAM address(内存地址)
10 5 pa variable value(变量值)
a b variable name(变量名)
_____________________________________________
char a = 10;
char b = 5;
a=11;
b=9;
//A memory address is required to store a bariable, and the address where the variable is stored is called a pointer
*/
int main() {
char a = 10;
//cout << (&a) << endl;
printf(“%#X”, &a); //0X7694F6A4
//c++ is compatible with C
//&a taking the address of variable a
//%X specifies that the integer should be output in upper case hexadecimal form
//# modifier makes the output hexadecimal number with the 0x prefix
return 0;
}
//9 – 2 the definiton and the use of pointer
#include <iostream>
using namespace std;
/*
____________________________________________
0X00000004 | 0X00000008 | …… | … | RAM address(内存地址)
10 5 pa variable value(变量值)
a b variable name(变量名)
_____________________________________________
*/
//If a variable stores a pointer to a piece of data, we call this variable a pointer variable
int main() {
int a = 10;
int b = 20;
//1.the definition of pointer variable
// data type * pointer variable name
//数据类型 * 指针变量名;
int* pa; // pointer a, there must be a * int when defining
pa = &a;
//pa = &b; //pointer can be sent values multiple
printf(“%#X %#X\n”,&a,pa); //the same addresses
cout << “—————–” << endl;
//2.dereference (解引用)
//to take the value of the address, and to modify
//*指针变量名 = 数值;
*pa = 7;
cout << a << ‘ ‘ << (*pa) << endl;
cout << “—————–” << endl;
//3.the relation between * and &
// *&a == *(&a) == *pa == a
//&*pa == &(*pa) == &a == pa
//they are essentially inverse operation of each other (实际上是个互逆的关系)
//likewise + –
//right combine : first calculate the right quote
cout << (*&a) << endl;
cout << (*(&a)) << endl;
cout << (*pa) << endl;
cout << (a) << endl;
cout << “—————–” << endl;
cout << (&*pa) << endl;
cout << (&(*pa)) << endl;
cout << (&a) << endl;
cout << (pa) << endl;
return 0;
}
//9 – 3 RAM memory of pointer
#include <iostream>
using namespace std;
//the pointer is a memory space itself, which can occupied the RAM space
int main() {
//sizeof(char);//1
//sizeof(int);//4
//sizeof(int *);//8
cout << sizeof(int*) << endl;
cout << sizeof(short*) << endl;
cout << sizeof(char*) << endl;
cout << sizeof(long*) << endl;
cout << sizeof(long long*) << endl;
cout << sizeof(float*) << endl;
cout << sizeof(double*) << endl;
//the pointer’s size only relative to the OS’s type(32bits,64bits)
//all the pointer type occupied the same size in an OS (32bits 4bytes,64bits 8bytes)
return 0;
}