C ++ entry notes

Source: Internet
Author: User

C ++ getting started learning notes pointer Source:Http://www.sudu.cn/info/html/edu/code/20071229/79821.html

 

 

Pointer Strengths:
1. Provides the function with the means to modify the call variable;
2. Supports Dynamic Distribution of sub-programs in C ++
3. Improves the efficiency of some sub-programs
4. Provides support for dynamic data structures (such as binary trees and linked lists)

Note: pointers introduce an indirect layer for a program, allowing you to manipulate pointers without directly manipulating objects.
1. Allows you to manipulate the addresses in the pointer or the objects indicated by the pointer.
2. The pointer may not point to any object. When writing * Pi, the program may be wrong during the execution period. If an object is addressed, the request is initiated. If the pointer does not point to any object, an error will occur, so before receiving the ticket, make sure that he points to an object.
A pointer that does not point to any object. Its contained address is 0, which is sometimes called a null pointer. Assert (P! = 0) check whether the allocation is successful. If (PI) is also available. It is true only when PI contains a non-zero value.

I. Definition:
Is the variable that stores the memory address.
Explanation:
A pointer also has its own address for a data type. 4-byte storage space
Int * P: & P returns the address of the pointer P, not the address of the variable.
Address: the location of another variable in the memory.

2. pointer variables:
The type * name must be compatible with the object type to be pointed.
Const is the principle of "closest"
Pointer to an integer constant: const int * P; the read-only value to which it points cannot be modified * P = 4 (error), P = 5 (correct)
Constant pointer to an integer: int * const P; the pointer variable value cannot be modified, * P = 5 (correct), P = 5 (incorrect)

Iii. pointer operators:
& (Addressing operator): The unary operator that acts on only one operand and returns the address of the operand.
* (Pull Operation): The unary operator, which is a complement operation of &. Return the value of the variable indicated by its operand.

Iv. pointer assignment and conversion:
Values of the same type are directly assigned, and conversion is required for different types.
Forced conversion: The expression result can be converted to a specified type.
Char * P; (int *) P forcibly converts P to int type. Remember to pay attention to the size of the two types during the conversion process. data may be lost in an hour (such as int to double)
Void:
In C, void * type can be assigned to any type of pointer, and vice versa
Mandatory conversion is required in C ++.
Void * can be used to accept values of any type for infinite power. Otherwise, it cannot be int * P = 9; void * t = P (correct); P = T (incorrect)

Forced conversion is required for all void * not involved.

5. arithmetic operations on pointers
Addition and subtraction of integers and their own increments and reductions
The increment Pointer Points to the next element of the same type as the pointer base. The increment or decrement unit is the length of the specified type.

Vi. Other Instructions:
1. Pointers and arrays:
Returns the starting address of the array, that is, the address of the first element of the array. Therefore, you can access the array in two ways: array subscript and pointer arithmetic.
2. Function pointer:
A function has a physical memory address that can be assigned to a pointer. A function address is also the entry point of the function and the address for calling the function.
3. Multilevel pointer address ** P

VII. Dynamic Memory Allocation
Definition: it is the method by which the program obtains the memory during running. Is to obtain the memory from the heap-free memory zone of the system
OPERATOR:
New (malloc in C): automatically creates an object of the appropriate size and returns a pointer of the correct type. If the allocation fails, a null pointer 0 is returned, the constructor can be called automatically.
Char * P = new char ('T ');
Delete (free in C): delect P;
When releasing an array object, use square brackets Delete [] P;

VIII. Differences from references
& Reference OPERATOR:
1. The reference is only the alias of the variable, rather than the pointer to the variable (different from the address-taking operator "&"), which does not occupy the memory space. The corresponding variable changes when the variable is referenced.
2. You cannot use the indirect pointer operator "*" to reference a reference.
3. The reference must initialize Int & C = count; (C is the alias of Count) during declaration)

9. Note:
Initialization should be performed before each pointer is used. To prevent pointers from pointing to null objects.

Application Example (pointer. cpp)
Compiling environment: window2000 vc6.0
# Include <string>
# Include <iostream>
Using namespace STD;

Void main ()
{
// Int * p = 1, no, the integer constant cannot be converted to an integer pointer, and char * T = 0 Can
// Pointer operator & returns the address of the operand. Here & P, & Q is the address of p and q.
// The address to be returned must either be directly connected to P or Q or use & (* P), & (* q), and the pointer must also be
// It is a data type and has its own memory address of 4 bytes, 8 bits
Int * q, * P;
Int x = 1, y = 2;
Q = & X;
P = & Y;
Cout <"p" <& P <"" <& (* P)
<"" <P <"<* P <Endl;
Cout <"Q" <& q <"" <& (* q)
<"" <Q <"<* q <Endl;

// Assign a value to the pointer. The entire pointer contains the address and the object to which the pointer is directed.
Int * t;
T = Q;
Q = P;
P = T;
Cout <"p" <p <"" <* P <Endl;
Cout <"Q" <q <"" <* q <Endl;

// Assign a value to the object indicated by the pointer. The address remains unchanged.
Q = & X; // 1
P = & Y; // 2
Cout <"p" <p <"" <* P <Endl;
Cout <"Q" <q <"" <* q <Endl;

// Force type conversion
// Double * l;
// L = (double *) * q; // The value of Q is assigned to the Temporary Variable * T = 1.

* T = * q; // The value of Q is assigned to the Temporary Variable * T = 1
Cout <* t <Endl;
* Q = * P; // Q, * q = 2
Cout <* q <Endl;
//?? * P = * t; // Why * The P value is not changed
* P = * t;
Cout <* t <Endl;
Cout <"p" <p <"" <* P <Endl;
Cout <"Q" <q <"" <* q <Endl;

// Reference Usage
Int COUNT = 1;
Int & C = count; // declares that C is a reference of count. C is only the alias of Count and does not occupy the actual memory space.
Cout <"Reference ";
Cout <C <count <Endl;
// The referenced variable must be initialized during declaration.
// Int & T; (error)
// T = count;

// You cannot use the indirect pointer operator to reference a reference again. The reference is just the alias of a variable.
// He does not occupy the address space
// Cout <* C <Endl;

Int if = 10;
Const int * CIS = 0; // pointer to an integer constant. It is best to initialize all pointers.
Int * It = & if;
// * CIS = 100; (error), * CIS is a constant
CIS = it;
Cout <CIS <"" <* CIS <Endl;

Int * const ICS = & if; // constant pointer to an integer
* ICS = 10;
ICS = it;
Cout <ICS <"" <* ICS <Endl;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.