C ++ Getting Started Study Reflections

xiaoxiao2021-03-06  77

Pointer advantage:

1. Provide a means of modifying calls to the function;

2. Support C dynamic allocation subroutine

3. Can improve the efficiency of certain subroutines

4. Support for dynamic data structures (such as binary tree, linked list)

Note: The pointer introduces an indirectibility to the program, which can manipulate the pointer without directly manipulating the object.

1. The controlled pointer includes an object to be manipulated by the pointer.

2. When the pointer does not point to any object, when writing * pi, the program may make the program in the execution period, such as addressed to an object, then the operation, does not point to any object, will be wrong, so determine before the promotion It does point to an object.

A pointer that does not point to any object, an internal address is 0, sometimes called a NULL pointer, ASSERT (P! = 0) can detect whether to assign success. IF (Pi) can also be used, only when PI is non-zero value, only for TRUE.

First, definition:

To store the variable of the memory address.

Interpretation:

The pointer has its own address for a data type. Take up four bytes of storage space

INT * P: & P Returns the address of the pointer P, not the address of the variable

Address: Generally refers to the location of another variable in memory

Second, pointer variables:

TYPE * NAME declares must ensure that its type is compatible with the type of object to point to

Const is the principle of "closest"

Pointer to integer constant: const INT * P; It points to the value only cannot be modified * p = 4 (error), P = 5 (correct)

Pointing to a constant pointer to an integer: int * const p; does not allow the value of the pointer variable, * p = 5 (correct), p = 5 (error)

Third, the pointer operator:

& (Line Operator): One-dollar operator, only acts on an operand, return the number of operands

* (Profile Action): One yuan operator, is the supplement operation, returning the value of the variable indicated by its operand

Fourth, pointer assignment and conversion:

The same type is directly assigned, and the heterogeneous type is converted.

Forced conversion: You can turn the expression results to the specified type

Char * p; (int *) P Forced P to convert the P to INT, remember to pay attention to the two types of size during the conversion, and the large turn may have data loss (such as int to double)

Recommended by void *:

C in the Void * type can assign a value to any type of pointer, and vice versa

Forced conversion is required in C

Void * can be used as an infinity can accept any type assignment, and it will not be int * p = 9; void * t = p (correct); p = T (error)

It is necessary to force the void * to force the conversion

V. Arithmetic operation of the pointer

And integers, subtraction, self increment, reduce

When the pointer is incremented, the next element is the same as the pointer base class, and the increase or decrease unit is the length of the specified type.

Sixth, other instructions:

1. Pointers and arrays:

Without the subscript of the number of group names to return to the starting address of the array, that is, the address of the first element, there are two ways to access the array: array subscript and pointer arithmetic

2. Function pointer:

The function has a physical memory address that can be assigned to the pointer, and a function address is also the entry point of the function, but also the address of the call function.

3. Multi-level pointer address ** P

Seven, dynamic memory allocation

Definition: It is a way to obtain memory in the run. Is from a heap-system free memory area - acquisition memory

Operator:

NEW (Malloc in C): Automatically establish an object with a suitable size, returns a pointer with the correct type, such as the assignment is unsuccessful, returns an empty pointer 0, and can automatically call the constructor. Char * p = new char ('t');

DELETE (Free): delect P;

Use square bracket delete [] P when release array objects.

Eight, the difference from the reference

& Quote Operators:

1. Quote is just the alias of the variable, not a pointer to the variable (distinguishes that the address operator "&") does not account for memory space, and changes to variables will also change.

2. You cannot use a pointer indirect operator "*" to use the pointer "*"

3. The reference must initialize int & c = count; (c is the alias of count)

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

New Post(0)