C language pointer learning, C language pointer

Source: Internet
Author: User

C language pointer learning, C language pointer

I have learned C language for a long time, but I don't have a very clear understanding of the pointers in it. I have the opportunity to study it well and summarize the learned knowledge. The knowledge comes from the explanation of C language pointers.

I. pointer Concept

A pointer is a special variable. The value stored in the pointer is an address in the memory. To learn the pointer well, it is important to understand the four aspects of the pointer: the pointer type, the pointer type, the memory area pointed to by the pointer, and the memory area occupied by the pointer itself.

1. How to Determine the pointer type:

  

int *p;char *p;int **p;int (*p)[3];int *(*p)[4];

The preceding five statements indicate the pointer types: int *, char *, int **, int (*) [3], and int * (*) [4]. is it very simple, that is, to remove the variable name, and the rest is the pointer type.

2. Type pointed to by pointer

int *p;char *p;int **p;int (*p)[3];int *(*p)[4];

In the preceding five statements, the pointer points to the following types: int \ char \ int * \ int () [3] \ int * () [4]. Have you found the rule? It is to remove the variable name and one *, and the rest is the type pointed to by the pointer. Special emphasis: pointer types and pointer types are different concepts.

3. The memory zone or pointer value pointed to by the pointer

The pointer value is the value stored by the pointer itself. This value is 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, because all the memory addresses in the 32-bit program are 32-bit characters long.

The memory area pointed to by the pointer starts from the memory area represented by the pointer value, and 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 indicates 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, the pointer value is the first address of the memory area.

If the Pointer Points to someone, the address of the person will be assigned to the pointer.

4. Memory occupied by pointers

The pointer occupies a large amount of memory. Use sizeof (pointer type) to test one. On a 32-bit platform, the pointer occupies four bytes.

Ii. arithmetic operations on pointers

  

char a[20];int *p=a;p++;

The pointer p type is int *, and the Pointer Points to the int type. The initialization bit points to the integer variable a. Then the pointer p is added with 1, which is handled by the compiler as follows: it adds sizeof (int) to the value of p, and adds 4 to the 32-bit program. since the address is in bytes, the address of p points from the address of a to the high address after adding four bytes.

Because the length of the char type is one byte, p points to the four bytes starting from 0 in the array, and now points to the four bytes starting from the fourth byte.

We can use a pointer and a loop to traverse an array:

  

Int arr [20]; int * p = arr ;....... // code for (I = 0; I <20; I ++) {(* p) ++; P ++ ;}

In this example, the value of each unit in an integer array is added to 1. Since every loop has a pointer p, each loop can access the next unit of the array.

char a[20];int *p=a;......p+=5;

In this example, p is added with 5. From the compiler perspective, add the value of the pointer p to sizeof (int ), 5*4 = 20 is added to 32-bit hengxu. because the unit of the address is cross street, p points to the 20 bytes in the starting high byte direction. In this example, p points to the four bytes starting with the first 0th units of the array without adding 5. After adding 5, p points out of the valid range of the array. Although this problem may occur in the application, it can be syntactically. This shows the flexibility of pointers.

To sum up, after a pointer and an integer n are added, the result is a new pointer pnew. The pnew type and the pnew type point to the same type as the pold type. The value of pnew is n * sizeof (p-type) bytes higher than the value of pold. The addition is to move to the high byte, And the subtraction is to move to the low byte.

Three operators & and *

& Is the bitwise operator. * It is called an indirect operator in the book. The operation result of & a is a pointer, And the pointer type is a type plus *. The Pointer Points to the type of a, and the Pointer Points to the address of. * The result of p is the result pointed to by p, and the type is p. The occupied address is the address pointed to by p.

Int a = 12; int B; int * p; int * ptr; p = & a // The result of & a is a pointer. the pointer type is an int *, the data type to which the Pointer Points is int and the address to which the Pointer Points is. * P = 24; // * p result, whose type is int. The address occupied by p is the address pointed to by p, obviously, * p is the variable a * ptr = & P; // p itself is a pointer. If p is getting an address, the pointer type is int **, the next type known to the pointer is int *. The Pointer Points to the p address ** ptr = 34; // * ptr returns what ptr points. Here is a pointer, which is a * operation on this pointer ,. The result is an int type variable.
Four-pointer expression

If the final result of an expression is a pointer, this expression is called a pointer expression.

  

Int a, B; int array [10]; int * pa; pa = & a; // pointer expression int ** ptr = & pa; // expression * ptr = & B; // expression pa = array; pa ++; // expression char arr [20]; char ** parr = arr // If arr is regarded as a pointer, arr is also a pointer expression char * str; str = * parr; // pointer expression str = * (parr + 1); // pointer expression

Since the result of a pointer expression is a pointer, the pointer expression also needs to have four elements of the pointer: pointer type, the Type pointed to by the pointer, the memory area pointed to by the pointer, and the memory area occupied by the pointer.

The pointer indicates a value on the left and the pointer indicates a value on the right.

To be continued
C language pointer Learning

It is difficult to use pointers, but it is efficient. For example, the float type occupies 4 bytes, And the pointer is always two bytes. It can be used to increase the speed, especially for functions. To be honest, if you can access the Internet, you can directly find the courseware and find the pointer chapter. Passing .... For example, I will not talk about it. If I want to learn it, I will read the book first. (I think you think it is a waste of expression to use pointers like I did before.) It is good for you to be patient, pointer is a difficult point, and spending more time ..

What is a good way to learn the pointer part of C language? It's really hard !!

The essence of C-pointer

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

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 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.
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 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?
... The remaining full text>

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.