C + + Constructs a data type----pointer

Source: Internet
Author: User

The construction data types constructed with basic data types are provided in C + + to describe and process complex data. These construction data types include a

Arrays, structs, unions, pointers, references. Next, I'll say pointers with my knowledge of pointers.

First, the origin of the pointer

To understand pointers, you have to look at what the address is.

When a variable is defined, the memory will be divided into a number of storage units consisting of a region, to hold the data of the variable, the

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 + +, pointer types are provided, and when a variable is defined as a pointer type, the address is stored in the variable. So that our image

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

Second, define the pointer

Whenever you store an address, you have to define a pointer variable.

define format as data type * variable name (pointer name)

The data type is the type of data that the pointer points to the memory cell. * is a representation symbol.

Third, how to use

1. Initialization

After the pointer is defined, only a pointer to the address can be stored, which holds a random number, so before using the pointer, it must be

Start.

2. Assign Value

The assignment format can be a data type * pointer variable name = initial address expression , or the pointer variable name = Address expression After the pointer is defined.

places to be aware of :

(1) The pointer is a storage address, do not assign non-address constants or variables and meaningless addresses to pointer variables.

(2) A pointer that has already been initialized can be assigned a value of the same type, and the type inconsistency can be cast. This is the same meaning as the normal variable assignment.

(3) If the basic data type of variables, arrays, etc. can be used & to get their own address, assigned to the same type of pointer.

Third, the application of the pointer

1. Relationships with strings

A character pointer is used to hold the address of a character variable.

A character pointer stores the address of the first character of the string, since this pointer points to the first address of the string, so the address of the other characters I

And we'll know about that.

Take a look at the relationship between pointers and strings from the following example:

Char *p_str= "ABCD", where the pointer holds the address of the character ' a ' in memory.

2. Relationship to arrays

Array of pointers

The array can hold elements such as strings, numbers, etc., and when the pointer is stored in an array, the array becomes an array of pointers.

The basic form is the type name * array name [subscript expression] For example: int * P_a[5]

Array pointers

The array is also addressed, and when the pointer holds an array address, the pointer becomes an array pointer.

The basic form is the type name (* pointer name) [subscript expression] For example: int * A_p[5]

4. Relationship with function

As a function parameter

Here, the pointer passes the data by address as a function parameter. When used as a function parameter, the pointer points to the in-memory data directly into the function's arguments.

pointer-type functions

A function that has a return value can return a value that is the value returned when the function is defined as a pointer to the type of the return value. Then this

function The name is the pointer name, and we call it a pointer-type function.

Pointers to functions

Function names are the starting addresses of functions in memory, and when you call a function, you find the address of the function in memory based on the function name, and then execute the function. This

, if the pointer holds the first address of the function, then that pointer is the pointer to the function.

5. Relationship with constants

Pointer constants

The name of the pointer is const-decorated, and the address stored in the pointer becomes a constant that cannot be modified.

Constant pointer

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

constant Pointer Constants

When the data type is const before and before the pointer name, it is clear that the above two merges to either modify the address stored in the pointer or to modify the contents of the memory cell pointed to by the pointer.

Iv. Summary

Pointer this piece of content I summarize is not very comprehensive, there are many things not written here, pointers in C + + programs play a very important role, if not a deep understanding of pointers, learning C + + is very difficult, it is easy to create confusion. I am also a beginner, there may be some places to understand the wrong, but also hope that everyone treatise. You are also welcome to communicate with me about the problem in the pointer!

C + + Constructs a data type----pointer

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.