In C, the pointer adds 1. Pointer variable details

Source: Internet
Author: User

A pointer is a special variable, and the value stored in it is interpreted as an address in memory . To make sense of a pointer, you need to understand the four aspects of the pointer:

    1. The type of pointer,
    2. The type to which the pointer is pointing,
    3. The value of the pointer, or the memory area that the pointer is pointing to,
    4. There is also the memory area occupied by the pointer itself.

Let's explain separately.

Let's declare a few pointers for example:
Example one:
(1) int *ptr;
(2) Char *ptr;
(3) int **ptr;
(4) int (*PTR) [3];
(5) int* (*PTR) [4];

Type of pointer


From a grammatical point of view, you simply remove the pointer name from the pointer declaration statement, and the remainder is the type of the pointer. This is the type that the pointer itself has. Let's look at the types of pointers in example one:
(1) The type of int*ptr;//pointer is int*
(2) The type of char*ptr;//pointer is char*
(3) The type of int**ptr;//pointer is int**
(4) int (*ptr) [3];//pointer is of type int (*) [3]
(5) int* (*ptr) [4];//pointer type is int* (*) [4]
What do you think? Is it easy to find out the type of pointers?

The type that the pointer points to


When you use a pointer to access the memory area pointed to by the pointer, the type that the pointer points to determines what the compiler will look for in that memory area .
Syntactically, you only have to remove the pointer name and the pointer to the left of the name in the pointer declaration, and all that remains is the type that the pointer points to. For example:
(1) The type that the int*ptr;//pointer points to is int
(2) The type that the char*ptr;//pointer points to is Char
(3) The type that the int**ptr;//pointer points to is int*
(4) int (*ptr) [3];//pointer to a type that is int () [3]
(5) int* (*ptr) [4];//pointer to the type is int* () [4]
  In the arithmetic operation of a pointer, the type that the pointer points to has a great effect.

  type of pointer (That is, the type of the pointer itself) and the type that the pointer points to are two concepts. As you become more familiar with C, you will find that the concept of "type", which is mixed with pointers, is divided into "types of pointers" and "types pointed to by pointers", which is one of the key points of mastery of pointers. I read a lot of books, found that some poorly written books, the pointer to the two concepts stirred together, so look at the contradiction between the book, the more confused look.


The value of the pointer, or the memory area or address that the pointer points to

The value of the pointer is the value stored by the pointer itself, which is treated as an address by the compiler, not 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 address from which the value of the pointer is represented, and the length of the memory area of sizeof (the type the pointer is pointing to). Later, we say that the value of a pointer is XX, which is equivalent to saying that the pointer to an XX-led address of a piece of memory area; we say that a pointer points to a region of memory, which is equivalent to saying that the value of the pointer is the first address of the memory area.

The memory area that the pointer points to and the type that the pointer points to are two completely different concepts. In example one, the pointer points to a type that already exists, but because the pointer is not initialized, the memory area it points to is nonexistent, or meaningless.

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

The memory area occupied by the pointer itself

How much memory does the pointer itself occupy? You just have to 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 when judging whether a pointer expression is an lvalue.


Arithmetic operations of pointers


The pointer can add or subtract an integer. The meaning of this operation of the pointer is not the same as that of the usual numerical subtraction operation. For example:
Example two:
1, Char a[20];
2, int *ptr = A;
...
...
3, ptr++;

In the example above, the type of the pointer ptr is int*, and the type it points to is int, which is initialized to point to the shaping variable A. In the 3rd sentence, the pointer ptr is added 1, which is handled by the compiler: it adds the value of the pointer ptr to sizeof (int), and in the 32-bit program, it is added 4. Since the address is in bytes, the address pointed to by PTR is incremented by 4 bytes from the address of the original variable A to the high address direction.
Since the length of the char type is one byte, the original ptr is the four bytes starting at Unit No. 0 of array A, pointing to the four bytes from array a starting at unit 4th.

We can use a pointer and a loop to iterate through an array, looking at an example:
Example three:
int array[20];
int *ptr = array;
...
Code that assigns values to an integer array is omitted here.
...
for (I=0;i <20;i++)
{
(*ptr) + +;
ptr++;
}
This example adds 1 to the value of each cell in an integer array. Because each loop has a pointer ptr plus 1, each loop has access to the next cell of the array.

Example four:

1, Char a[20];
2, int *ptr = A;
...
...
3, PTR + = 5;
In this example, PTR is added with 5, and the compiler handles this by adding the value of the pointer ptr to 5 by sizeof (int), which adds 5 times 4=20 to the 32-bit program. Because the unit of the address is byte, the current PTR points to an address that is 20 bytes to the high address direction than the address pointed to by PTR after 5. In this example, PTR, which is not added to 5, points to the four bytes starting at Unit No. 0 of array A, plus 5, and PTR points to the legal range of array A. Although this situation may be problematic in application, it is syntactically possible. This also shows the flexibility of the pointer.

In C, the pointer adds 1. Pointer variable details

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.