C Language Pointer use method

Source: Internet
Author: User
Tags numeric value

Service for beginners. This is the purpose of this article. Pointers are a difficult and important point in C and C + +. Some programmers are proficient in basic DOS. All the other features of the C language have something similar in basic. Only the pointer, is Baisc do not have. The pointer is the soul of C.

I don't want to repeat what most of the books say very clearly, I just write what I read in the book that is not clear or said, and I think I understand something that makes sense.

1, the concept of the pointer

A pointer is a special variable that is stored in a value that is interpreted as an address in memory. To figure out a pointer needs to figure out four aspects of the pointer: the type of the pointer, the type the pointer is pointing to, the value of the pointer, the memory area that the pointer points to, and the memory area occupied by the pointer itself. Let's explain separately.

Let's declare a few pointers to do the example:

Example one:

(1) int *ptr;
(2) Char *ptr;
(3) int **ptr;
(4) int (*PTR) [3];
(5) int * (*PTR) [4];

1, the type of the pointer

From a grammatical point of view, you simply remove the pointer name from the pointer declaration statement, and the rest is the type of pointer. This is the type that the pointer itself has. Let's look at the types of each pointer in example one:

(1) int *ptr;
The type of the pointer is int*
(2) Char *ptr;
The type of the pointer is char*
(3) int **ptr;
The type of the pointer is int**
(4) int (*PTR) [3];
The type of the pointer is int (*) [3]
(5) int * (*PTR) [4];
The type of the pointer is int* (*) [4]

What do you think? How easy is it to find the type of pointer?

2, the type that the 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 the compiler treats the contents of that memory area as.

Syntactically, you simply remove the pointer's name from the pointer declaration statement and the pointer to the left of the name, and the rest is the type that the pointer points to. For example:

(1) int *ptr;
The type that the pointer points to is int
(2) Char *ptr;
The type that the pointer points to is Char
(3) int **ptr;
The type that the pointer points to is int*
(4) int (*PTR) [3];
The type that the pointer is pointing to is int () [3]
(5) int * (*PTR) [4];
The type that the pointer points to is int* () [4]

In the arithmetic operation of a pointer, the type that the pointer points to has a significant effect. The type of the pointer (that is, the type of the pointer itself) and the type to which the pointer points are two concepts. As you become more and more familiar with C, you will find that the concept of "type", which is mixed with pointers, is divided into "type of pointer" and "type of pointer", which is one of the key points in mastering pointers.

3, the value of the pointer, or the memory area or address to which the pointer points

The value of a pointer is a numeric 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 example one, the type that the pointer points to is already there, 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 point to? Where does the pointer point to?

4, the memory area occupied by the pointer itself

How much memory does the pointer itself account for? You just use the function sizeof (the type of the pointer) 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.

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.