C++ Primer Plus Notes 3 (Chapter Four-Pointers)

 The * operator in front of the pointer is called the indirect value or dereference operator.

1. Pointer declaration in C++ : int* ptr; // Emphasis that int* is a type-pointer to int . ptr itself is a pointer

                                int* p1, p2; //Declare a pointer p1 and an int variable p2.

2. ** Pointer initialization : ( not to initialize the value it points to ) int h=5; int* pt= &h; [Pointer initialization is very important] (delete pt; //not allowed)

    The following statement: long *fellow; *fellow=23323; //A bug will occur, it is impossible to determine where the fellow points to, and it may not point to the place where 23323 is stored

3. The pointer is not an integer: int* pt; pt=0xB8000000; //Illegal 

                               int* pt; pt=(int *) 0xB8000000; //legal

4. Use the library function malloc() to allocate memory in C. The same can be done in C++. There is another method-the new operator. new and delete should be used in pairs

  int * pn=new int; ....delete pn; //Use delete to release the memory allocated by new

double * pd=new double; *pd=10000001.0; //size of pd =4, size of *pd=8, the length of the pointer is 4 bytes, it has nothing to do with the pointed data type

5. Use new to create a dynamic array:

double * p3=new double [3];   //Create an array containing 3 doble elements, the pointer p3 points to the first element in the memory

p3[0]=0.2; p3[1]=0.5; p3[2]=0.8; //Pointer access to elements in the array

p3=p3+1; cout<<p3[0]<<" "<<p3[1]; //The output result is that p3[0] is 0.5 and p3[1] is 0.8. p3+1 causes the pointer to point to the second element.

p3=p3-1; delete [] p3;   //When the pointer is released, it should be decremented by 1, pointing to the original value. Add [] when releasing array memory

6. After the pointer variable is incremented by 1, the increment is equal to the number of bytes of the type it points to.

In most cases, C++ interprets the array name as the address of the first element of the array.

When using array notation, C++ performs the following conversion: array[i] --> *(array+i)

Pointers are used instead of array names. C++ also performs conversion: pt[i]--> *(pt+i) 

7. Apply sizeof to the array to get the length of the array; apply sizeof to the pointer to get the length of the pointer (=4);

8. Array address

short tell[10];  

cout<<tell<<endl;  //显示&tell[0]的结果,是一个2字节的内存块地址

cout<<&tell<<endl; //对数组名取地址(&)显示整个数组的地址,是一个20字节的内存块地址

//tell是一个short指针 *short
//&tell是一个指向包含20个元素的short数组的指针 short(*)[20]

short (*pas)[20]=&tell; //pas指针指向含20个short元素的数组
//*pas等价于tell, (*pas)[0]=tell[0];

9. If you provide a pointer to cout, it will print the address, if the pointer type is char*, then cout will display the character pointed to.

   If you want to display the string address, you must cast (int *) ps.

 char animal[20]="fox";        char *ps;

ps=animal; cout<<ps<<" at "<<(int *) ps<<endl; //ps is a pointer, but displays a string, (int *) ps displays the address of the string

10. Get a copy of the string array by using new and strcpy()

  ps=new char[strlen(animal)+1];   

  strcpy(ps,animal); //Cannot assign directly, ps=animal, just want to copy the address to ps, the two pointers point to the same memory unit and string, no string is copied.

11. When using new to create a dynamic structure, the pointer pa represents a pointer to the structure, and the way to access structure members: (1) pa->name; (2) (* pa).name [name is a structure member]

12. 

struct Years
{
 int year;
 int month;
};
Years y1,y2,y3; //创建结构变量
Years ys[3];    //创建结构数组


const Years * apy[3]={&y1,&y2,&y3};  //创建指针数组

//创建上述指针数组的指针,两种方式
 const Years ** ppya=apy;   //方法1
 auto ppyb=apy;             //方法2,方便一点,不易搞混
//访问
std::cout<<(*ppya)->year<<std::endl;
std::cout<<(*(ppyb+1))->yrar<<std::endl;

13. Template class vector class (dynamic array)  : The application is to add header files #include <vector>;

using spacename std;    vector<int> vi(n); // A vector object named vi, which can store n elements of type int, n can be an integer constant or an integer variable

14. The template class array class  (C++11) application is to add header files #include <array>; Like arrays, array objects have a fixed length.

using spacename std;    array<int,5> ai; //An array object named ai, which can store n elements of type int, n is an integer constant., here n=5;

15. Arrays, array objects, vector objects can use standard array notation to access each element.

The array object can be directly assigned to another array object, and for an array, the data must be copied element by element.

Array objects and arrays are stored in the same memory area (ie stack); vector objects are stored in another area (free storage area or heap).

 

 

 

 

Guess you like

Origin blog.csdn.net/lvliang2017232003/article/details/85341687