C ++ constructs data types ---- pointers, data types ----

Source: Internet
Author: User

C ++ constructs data types ---- pointers, data types ----

 

C ++ provides the construction of data types constructed with basic data types to describe and process complex data. These constructed data types include

For example, array, structure, union, pointer, and reference. Next, let's talk about pointers based on my understanding of pointers.

 

I. pointer Origin

 

To understand the pointer, you must first check what the address is.

When a variable is defined, the memory is divided into several storage units, which are used to save the data of the variable.

Indicates that each storage unit in the memory has its own number, which is called the address. These addresses are also data. Where are they stored? In

In C ++, the pointer type is provided. When a variable is defined as a pointer type, the address can be saved to this variable. In this way, our image

The variable that can store the memory unit address is called a pointer.

 

Ii. Define pointers

 

Every time an address is stored, a pointer variable must be defined.

The definition format isData Type * variable name (pointer name)

The data type refers to the data type pointing to the memory unit. * is a symbol.

 

Iii. How to Use

 

1. Initialization

After the pointer is defined, only a pointer that can store the address is obtained, which stores a random number. Therefore, before using the pointer, you must

Start-up.

 

2. Assignment

The value assignment format can beData Type * pointer variable name = initial address expression; You can also define the pointer,Pointer variable name = address expression.

 

 

Note:

(1) the pointer stores the address. Do not assign non-address constants, variables, or meaningless addresses to the pointer variable.

(2) initialized pointers can assign values to pointers of the same type. If the types are inconsistent, they can be forcibly converted. This is similar to assigning values to common variables.

(3) If a variable or array of the basic data type can use & to obtain its own address, assign a value to a pointer of the same type.

 

Iii. pointer Application

 

1. Relationship with strings

The reserved pointer is used to store the reserved variable address.

A character pointer stores the address of the first character of the string. Because the Pointer Points to the first address of the string, the address of other characters is

You can also know.

The following example shows the relationship between pointers and strings:

Char * P_str = "abcd", the pointer stores the address of character 'a' in memory.

 

2. Relationship with Arrays

Pointer Array

The array can store elements such as strings and numbers. When a pointer is saved to an array, this array becomes a pointer array.

The basic form isType name * array name [subscript expression] example: int * p_a [5]

 

Array pointer

An array also has an address. When the pointer stores an array address, this pointer becomes an array pointer.

The basic form isType name (* pointer name) [subscript expression] example: int * a_p [5]

 

 

4. Relationship With Functions

As function parameters

Here, the pointer is used as a function parameter to transmit data by address. As a function parameter, the Pointer Points to the data in the memory and directly becomes a function parameter.

Pointer Functions

A function with a return value can return a value. When the return value type is defined as pointer type, the return value is an address. In this case

The function name is the pointer name. We call this function a pointer function.

Pointer to function

The function name is the starting address of the function in the memory. when calling the function, find the address of the function in the memory based on the function name, and then execute the function. This

If the pointer contains the first address of the function, the pointer is the pointer to the function.

 

5. Constant relationship

 

Pointer constant

The address stored in the pointer becomes an unchangeable constant.

 

Constant pointer

When const is added before the data type, the content of the memory unit pointed to by the pointer cannot be modified. However, you can modify the address stored in the pointer.

Constant pointer constant

When const is added before the data type and before the pointer name, it is clear that the preceding two operations cannot modify the address stored in the pointer or the content in the memory unit pointed to by the pointer.

 

Iv. Summary

The content of pointer is not comprehensive yet. There are still many things not written here, And pointer plays a very important role in the C ++ program, if you cannot fully understand pointers, it is very difficult and confusing to learn C ++. I am also a beginner. I may not understand it in some places. You are also welcome to share with me questions about pointers!

 

 

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.