1.1 first recognized pointer, 1.1 first recognized

Source: Internet
Author: User

1.1 first recognized pointer, 1.1 first recognized

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 four aspects of the pointer: the pointer type, the pointer type, the pointer value, or the memory zone pointed by the pointer, there is also the memory zone occupied by the 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 perspective of syntax,

You only needSetTheThe pointer name is removed, 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 needSetIn the pointer declaration statementThe pointer name and the pointer declarative * on the left of the name are removed, 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 Pointer Points to an int type *
(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 ValueOrMemory zone or address pointed to by the pointer 
The pointer value isThe value stored by the pointer.The value will be treated as an address by the compiler, rather than a general value.In a 32-bit program, the values of all types of pointers are a 32-bit integer.Because all the memory addresses in the 32-bit program are 32-bit long.Memory area to which the Pointer PointsYesStarting from the memory address represented by the pointer value, the length is a memory area of sizeof (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 headed by XX;We say that a pointer points to a memory area, which means 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?


4. Memory occupied by the pointer itself
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 is left.

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.