Comprehensive analysis of the concept of C pointer

Source: Internet
Author: User

C language pointer Concept

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];

If you cannot understand the last few examples, refer to the articles I posted earlier < <如何理解c和c 的复杂类型声明> >.

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?

Type pointed to by C 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 Pointer Points to an int type.) [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.

Pointer type) and pointer type are two concepts. When you get familiar with C, you will find that, the concept of \ "Type \" that is stirred up with pointers is divided into two concepts: \ "pointer type \" and \ "pointer type, it is 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.

The 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 is a memory area starting from the memory address represented by the pointer value, and the length is the type pointed to by the si zeof 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?

Memory occupied by the C language pointer itself

How much memory does the pointer occupy? You only need to use the sizeof 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.

  1. Shortcuts to learning C Language
  2. Five Reasons for every programmer to learn C Language
  3. Detailed description of the role of Static in C/C ++

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.