The C ++ Programming Language Chapter 5

xiaoxiao2021-03-06  42

The C Programming Language chapter study notes james chen / 050220 ********************* 5.1 pointer ************** *******

For type T, t * is "to T, pointers", that is, a variable of a type T * can save an address of an object of T. Char c = 'a'; char * p = & c; // Send C's address to CHAR pointer PCOUT << c << "," << * P << Endl; // two values ​​* p = ' h '; // Indirect modification C because p = & c, (* p =' h ') == (c =' h ') cout << c << endl; // h

Very depressed: to the t *, function pointer, pointer array, play more complicated,, go:

First, to the pointer of the T * [to (T type pointer), this is better to understand.

INT i = 123; INT * P1 = & I; // int ** P2 = p1; // wrong, P1 only one *, so you can't assign int ** p2 = & p1; // correct, take the address of the P1 pointer to P2, equivalent to int **, just matching COUT << & i << endl; cout << p1 << endl; // p1 and i address same COUT << & p1 << endl; // & p1 is P1 pointer Address, different from P1, P1 is the address cout << p2 << endl; // & p2 is the address of the P2 pointer, the same // cout << * p2 << endl; // error, * P2 is just the address cout << * (* p2) << endl; // correct, obtain the value of the object of the P2 pointer // can also be written into ** p2 ** p2 = 321; // Pointer assignment cout << i << endl; // 321cout << * p1 << endl; // 321

Extended learning: Pointer to pointers of pointers of T *'s pointer ... int A = 123; int * b = & a; int ** c = & b; int *** d = & c; int *** E = & D; int **** f = & e; ***** f = 54321; // 54321cout << a << endl; // 54321cout << * b << endl; // 54321cout << * * c << Endl; // 54321cout << *** D << endl; // 54321cout << **** E << endl; // 54321cout << ***** f << endl; // 54321 // Cool, it turns out that this is the case, it should continue. . . Second, the pointer array, that is, the array element is a pointer.

INT * a [10]; // Declare an array, contains 10 elements, for INT type pointer for (int i = 0; i <10; i ) a [i] = new int (100 i); / / Apply for INT memory space, and assign a value of 100 i, then give the address to a [i] for (i = 0; i <10; i ) cout << a [i] << endl; // display array Element (I = 0; i <10; i ) cout << * a [i] << Endl; // display value, is 100, 101, 102, 103 .... 109

Third, pointers and functions

1, pointer as a function of the parameter void hehe (int * x) {* x = 100;} int A = 23; Hehe (& a); cout << a << endl; // 123; int * b = new int a); // Apply for a memory space, assign a value of AHEHE (B); cout << * b << endl; // 223;

2, the pointer function, that is, a pointer INT * HAHA (INT * x) {* x = 100; returnx x;} int a = 1; int * b = haha ​​(& a); // will return the address returned to * bcout << * b << Endl; // 101cout << Haha (b) << endl; // 0x00 .... Display Returns the pointer address, here as the B address is the same cout << * b << end1; // 201cout << * Haha (b) << Endl; // 301INT C = * Haha (b); // Take the return address * Haha (b), then deliver the value to the INT variable Cout << c <<

INT ADD1 (INT A); INT Add2 (INT A) {RETURN A * 100;} INT Add3 (INT A) {Return A * 1000;} Add = add1; int i = 2; int a = add (i); add = add2; int b = add (i); add = add3; int C = add (i); cout << a << "," <

Note: Function pointers and function pointers are just pointers, not a function, so don't engage in a function of painting snakes.

// cool, this can be tried to try it, at least there is no such thing in the book, of course, it is definitely too little book.

******************** 5.2 arrays *******************************

Initialization INT A [3] = {1, 2, 3}; // correct / {INT A [3] = {1, 2, 3, 4}; // Error, array a only three elements INT A [5] = {1, 2}; // is correct, equivalent to INT A [5] = {1, 2, 0, 0, 0}; int A [] = {1, 2, 3, 4, 5, 6}; // correct A [] = {1, 2, 3, 4}; // error, give a non-existing array assignment 5.2.2 String text quantity string text quantity is included in two double quotes Character sequence, such as: "How are you !!"

The actual length of a character text is more than one of the characters it contains, because it is always ending by '/ 0', such as Szieof ("Love") == 5

The type of string text is "an array of appropriate const characters", so "Love" type is actually const char [5]. You can use string text to give a char * assignment, but you can't use this char * to modify string text volume, such as: char * a = "How are you !!"; cout << a << endl; // ~ Are you !! // a [0] = 'a'; // fault, assign a constant, but the compiler will not prompt

Remember: Put the string of characters as a constant.

If you want to modify the string, you need to copy it to a char number: char a [] = "How are you !!"; cout << a << endl; // how are you !! a [3] = 'a'; cout << a << endl; //howaare you !!

The string of string is static allocation, so you can return to: const char * aa () {return "haha";} const char * b = aa; cout << b << endl; // haha

In order to make the program clean, you can divide a long string into several parts by blank characters, such as: char * p = "AAAAAA" "BBBBBB" "CCCCC"; cout << p << endl; // aaaaaa bbbbb ccccc

********************* 5.3 Pointers and arrays *********************

In C , the pointer is closely related to the array. The name of an array is the first address of the array, and it can assign a value to the same type of pointer. Char x [10] = "aaabbbccc"; char * a = x; // Assign X's first address to * achar * b = & x [3]; // Assign the address of the fourth element of X * bchar * C = & x [6]; // 将 将 地址 第 地址 地址 地址 * ccout << a << endl; // aaabbbcccout << B << Endl; // bbbccccout << c << endl; // CCC5.3.1: Tour in array access arrays can pass the subscript and pointer: char A [5] = "abcd"; for (int i = 0; i <4; i ) // i <4 can also Changed to * b! = 0, end compliance is 0cout << a [i] << endl; // with the next mark

Char * b = a; for (i = 0; i <4; i ) // i <4 can also be replaced with * b! = 0, end compliance is 0cout << * b << endl; // traversing Array

Char * c = a; while (cout << * C << Endl, * C! = 0); // use the pointer over the group

It is meaningful if the two pointers points to the same array element. If another pointer is subtracted from a pointer, the result is the number of array elements between the two pointers (an integer). Pointer or minus an integer, the result is still a pointer, and if the resulting pointer address exceeds the array boundary, then this value is unfained.

INT A [10]; INT B [10]; INT i = & a [5] - & a [2]; cout << I << endl; // 3, interval 3 elements i = & a [5] - & b [[ 2]; cout << i << endl; int * p = a 2; // p = a [2]; p = a-2; // out of bounds, * p is not defined

Depressed, this out-of-band pointer can be used, blame: cout << * p << endl; // random value * p = 4321; cout << * p << endl; // 4321 ***** *************** 5.4 constant ************************* constant is not changed, once declaration, Can be used directly, but it cannot be modified. The constant statement must be initialized.

Const int A = 123; const Int b [] = {4, 3, 2, 1}; const INT C; // error, not initialization a = 200; // error, constant cannot modify B [2] = 34; / / Error, constant cannot be modified

Constants is mainly used in some initialization, such as: PI circumference rate, fixed value, no change. The value of some objects. Functional ginseng, read reading is not written. INT add100 (const INT A) {a = 100; // error, does not change A of the value RETURN A 100; // OK, the value of A did not change A}

5.4.1: The pointer and constant use a pointer to two objects: the pointer itself and the pointer to the pointer.

Const * will make the object a constant. // Individual understands: The pointer constant * const will make the pointer a constant. // Individual understanding is: constant referring to the context: int = 100; int o = 123;

// const * int const * p = & i; // Make the object into constant cout << * p << endl; // 100 * p = 123; // fault, object is constant, can not assign a value P = & O; // OK, pointer is not constant, you can change the address cout << * P << endl; // 123

// * constint * const p1 = & i; // often pointer cout << * p1 << Endl; // 100 * p1 = 123; // OK, the object is very quite, can be changed << * p1 << endl; // 123p1 = & o; // Failure, pointer is constant, can not change const INT * const p2 = & o; / / to const pointer * p2 = 33; // wrong, P2 points to constant p2 = & I; / / Wrong, P2 itself is also a constant cout << * p2 << Endl; //123********************************* ************* The reference is another name of the object. The primary use of the reference is to describe the parameters and return values ​​of the function, and operator overload. INT A = 123; INT & B = a; int C = B; cout << a << "," << b << "," << c << Endl; // 123, 123, 123b = 234; cout << " A: "<< a; // 234, because B references A, B = 234 is equivalent to a = 234

A referenced address cannot change after initialization, which always points to the object referred to when initialization. Therefore, it is very cool to use as a shape.

Note: You cannot assign a reference declaration. INT & m = 234; // Wrong, regular reference requires a memory unit address, so it cannot be directly assigned.

However, you can use const t & to assign a value directly at the declaration: const INT & M = 234; Const T & Method: 1, right value to T type implicit conversion. 2. Place the right value into a Type T type. 3. This temporary variable address is used as an initial value. This temporary variable has always been present until this reference is ended.

Bjarne Stroustrup Advice:. In order to improve the readability of the program, you should usually avoid the function to modify their parameters as much as possible. If the "normal" reference parameter is used for some functions, the names of these functions should give a strong prompt that its reference parameters will be modified.

******************************************************************************************************

One pointer to any object type can assign a value to the type VOID * variable, and the void * can assign a value to another void *, two VOID * can be equal or not, and can explicitly convert VOID * to another type. .

INT * p = new int (123); void * a = p; // assignment to the Void pointer void * b = a; // Void pointer assignment to another Void pointer cout << a << endl; cout << B << endl; cout << p << endl; // three addresses Cout << * a << endl; // error, can not indirectly reference A ; // error, can't increase, do not know a size INT * t = a; // error, does not implicitly convert int * t = static_cast (a); // OK, the most important purpose that can be explicitly converted back to INTVOID * is to pass a pointer to the function And don't make any assumptions to the type of object. There is also a typeless object that returns from the function, of course, to use the object, the type conversion must be displayed.

/ * Personal understanding: After assigning an object to the Void pointer, you can see that two addresses are the same, I think it should be understood that the Void pointer is a reference to the object, but the type is unknown, the VOID pointer cannot be used *, , etc., otherwise it will cause an error. * /

********************* 5.7 Structure ******************** Array is the gathering of the same elements And stuct is a gathering of any type of element. Struct address {char * name; int id;};

The same is the same as that of Struct's variable declaration and other types: address a; a.name = "i love bb !!"; A.ID = 4321; initialization of structural type variables can also be like array: address b = {"HEHE" 321};

Use of structural pointers: address * c = & a; cout << c-> name << Endl; // i love bb !! cout << c-> id << Endl; // 4321cout << (* c) .name << Endl; // i love bb !! cout << (* c) .id << endl; // 4321address d = a; // Struct assignment to structd = * c; // struct * assignment to Struct

Pointers -> Members == * Pointers when used for Struct Type Pointers

********************* 5.8 BS advice ******************** 1. Avoid Ordinary pointer technology. 2. Beware, don't write over the boundaries of the array. 3. Try to use 0 rather than NULL. 4. Try to use Vector and Valarray instead of an array. 5. Try to use String instead of the CHAR array ended with 0. 6. Use less common reference parameters as little as possible. 7. Avoid VOID *, in addition to in some low-level code. 8. Avoid using non-plain text quantities ("Mysterious Number") in your code, should define and use various symbol constants.

转载请注明原文地址:https://www.9cbs.com/read-55366.html

New Post(0)