Let you no longer fear pointer 1

Source: Internet
Author: User

 

A pointer is a special variable. The value stored in it is interpreted as an address in the memory. To understand a pointer, we need to understand the four aspects of the pointer: pointer type, pointer type, pointer value, memory area pointed to by pointer, memory area occupied by pointer itself. Let's explain it separately.

 

First, declare several pointers for example:

 

Example 1:

 

(1) int * ptr;

 

(2) char * ptr;

 

(3) int ** ptr;

 

(4) int (* ptr) [3];

 

(5) int * (* ptr) [4];

 

1. pointer type

 

From the syntax perspective, you only need to remove the pointer name in the pointer declaration statement, and the rest is the pointer type. This is the type of the pointer. Let's take a look at the type of each pointer in Example 1:

 

(1) int * ptr; // the pointer type is int *

 

(2) char * ptr; // the pointer type is char *

 

(3) int ** ptr; // the pointer type is int **

 

(4) int (* ptr) [3]; // the pointer type is int (*) [3]

 

(5) int * (* ptr) [4]; // the pointer type is int * (*) [4]

 

How is it? Is it easy to find the pointer type?

 

2. Type pointed to by pointer

 

When you access the memory area pointed to by the pointer, the type pointed to by the pointer determines what the compiler will regard the content in the memory area.

 

In terms of syntax, you only need to remove the pointer name and the pointer declarative * on the left of the name in the pointer declaration statement, and the rest is the type pointed to by the pointer. For example:

 

(1) int * ptr; // The Pointer Points to an int type.

 

(2) char * ptr; // The Pointer Points to a char type.

 

(3) int ** ptr; // The type pointed to by the pointer is int *

 

(4) int (* ptr) [3]; // The type pointed to by the pointer is int () [3]

 

(5) int * (* ptr) [4]; // The type pointed to by the pointer is int * () [4]

 

In pointer arithmetic operations, the type pointed to by the pointer has a great effect.

 

The pointer type (the pointer type) and the pointer type are two concepts. When you get familiar with C, you will find that, the concept of "type", which is mixed with pointers, is divided into two concepts: "pointer type" and "pointer type", which are one of the key points of mastering pointers. I have read a lot of books and found that some poorly written books bring together the two concepts of pointers. Therefore, the books seem to have conflicts and become more confused.

 

3. pointer value ---- or the memory zone or address pointed to by the pointer

 

The pointer value is the value stored by the pointer itself. This value will be treated as an address by the compiler rather than a general value. In a 32-bit program, the value of all types of pointers is a 32-bit integer, because the 32-bit program's memory address is all 32-bit long. The memory area pointed to by the pointer starts from the memory address represented by the pointer value, and the length is a memory area of si zeof (type pointed to by the pointer. Later, we will say that the value of a pointer is XX, which means that the pointer points to a memory area with XX as the first address. We will say that a pointer points to a memory area, it is equivalent to saying that the pointer value is the first address of the memory area.

 

The memory zone pointed to by the pointer and the type pointed to by the pointer are two completely different concepts. In example 1, the pointer points to a type that already exists, but since the pointer has not been initialized, the memory zone it points to does not exist, or it is meaningless.

 

In the future, every time you encounter a pointer, you should ask: What is the type of this pointer? What is the pointer type? Where does this pointer point? (Important)

 

4. Memory occupied by pointers

 

How much memory does the pointer occupy? You just need to use the sizeof function (pointer type) to test it. On a 32-bit platform, the pointer occupies 4 bytes.

 

The memory occupied by pointers can be used to determine whether a pointer expression (which will be explained later) is a left value.

Related Article

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.