On the essence of C + + pointer (i.)

Source: Internet
Author: User
Tags data structures


Summary



Pointers are one of the most important concepts in C and C + + programming, and one of the most confusing and error-prone programs. The use of pointer programming can represent a variety of data structures, through pointers can be used between the main function and the adjusted function to share variables or data structures, easy to achieve two-way data communication, and can be like assembly language processing memory address, so as to compile a concise and efficient procedures. Pointers greatly enrich the capabilities of the C and C + + languages.



In this article, the pointers are mainly discussed in two parts. First, the basic discussion about the content and operation of pointers, can be the reader's knowledge of the pointer and understanding; Then in the use of the article focused on the various applications of pointers, disclosure pointers in the day-to-day programming of the essence, so that readers can really understand, recognize and use pointers.



First article: Basic article



1.1 The concept of pointers



When it comes to pointers, it's flexible and difficult to control so many programmers Tanhusebian; but it's directly manipulating the memory in the data



The operation has a fast, save memory and other advantages, but also many C + + programmers love not to. So what exactly is a pointer to a concept?



In fact, a pointer is a class of variables, a class of variables that contain the addresses of other variables or functions. Unlike other variables, the generic variable contains the actual real data, and the pointer is an indicator that tells the program where to find the data in the area in which it is stored.



Well, here we can define pointers: pointers are variables that contain the addresses of other variables or functions, and the values stored inside them are interpreted as memory addresses.



1.2 The contents of the pointer



In short, the pointer has four aspects: the type of the pointer, the type the pointer points to, the value of the pointer, the pointer itself



The memory area occupied. Here's what we'll explain separately.



Type of 1.2.1 pointer



From a grammatical point of view, the type of the pointer refers to removing the pointer name from the pointer declaration statement to remove the remainder. This is the type that the pointer itself has. For example:



Int *ip; //The type of the pointer is int*
Char *ip; //The type of the pointer is char*
Int **ip; //The type of the pointer is int**
Int (*ip)[5]; //The type of the pointer is int(*)[5]



The type that the 1.2.2 pointer points to



When you access the memory area that the pointer points to by using the pointer, the type that the pointer points to determines what type of content the compiler will look at in that memory area. From a grammatical point of view, the type that the pointer points to is the pointer's name in the pointer declaration statement and the Pointer declaration character to the left of the name. * Remove the remainder. For example:



Int *ip; //The type pointed to by the pointer is int
Char *ip; //The type pointed to by the pointer is char
Int **ip; //The type pointed to by the pointer is int*
Int (*ip)[5]; //The type pointed to by the pointer is int()[5]



The value of the 1.2.3 pointer (or the memory area to which the pointer points)



The value of the pointer, or the memory area or address that the pointer points to, is the value stored by the pointer itself, which is treated as an address by the compiler, not as a generic value. In a 32-bit program, the value of all types of pointers is a 32-bit integer, because the memory address in the 32-bit program is all 32 bits long. The memory area that the pointer points to is the memory area that begins with the memory address represented by the value of the pointer, and the length of the sizeof (the type to which the pointer points). Later, we say that the value of a pointer is XX, it is equivalent to say that the pointer to a piece of memory in the address of XX; we say a pointer to a block of memory is equivalent to saying that the value of the pointer is the first address of the area of memory.



The memory area pointed to by the pointer and the type pointed to by the pointer are two completely different concepts. In the example above, the type pointed to by the pointer already exists, but because the pointer has not yet been initialized, the memory area it points to does not exist, or is meaningless.



Later, every time you encounter a pointer, you should ask: What is the type of the pointer? What type does the pointer refer to? Where does the pointer point to?



The memory area occupied by the 1.2.4 pointer itself



The memory area that the pointer itself occupies is the size of the pointer itself, which you just use the function sizeof (the pointer's



Type) to find out. In a 32-bit platform, the pointer itself occupies a length of 4 bytes.



The concept of memory occupied by the pointer itself is useful in determining whether a pointer expression is a left value.


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.