C language pointer Summary

Source: Internet
Author: User

What is the essence of C language and the pointer, which is also the only difficulty in C language.

C is a very convenient language for underlying operations, and the most commonly used in underlying operations is pointers. In the future, pointers will accompany us for life.

This article will look at the pointers in C language from eight common aspects. Of course, there are other aspects not specifically mentioned, such as pointer expressions and pointer security, I will try again later.

In the old saying, the most important thing is practice and writing more.CodeIs the key to learning C language well.

 

1.Pointer Type Analysis

Analysis pointer, which can start from the variable name and be analyzed step by step based on the combination of operator priorities.

Int P;// This is a common integer variable

Int * P;// Start with P and combine with * first, so P is a pointer and then int, indicating that the type of content pointed to by the pointer is int. so P is a pointer to return integer data.

Int P [3];// Start with P and first combine with [], indicating that P is an array and then combine with int, indicating that the elements in the array are integer, therefore, P is an array composed of integer data.

Int * P [3];// Start with P and combine it with [] First. Because the priority is higher than *, P is an array and then combined, the elements in the array are pointer types, and then combined with int, indicating that the content pointed to by the pointer is of an integer type, so it is an array composed of pointers that return integer data.

INT (* P) [3];// Start with P, and first combine with *, indicating that p is a pointer and then combine with [] (This step can be ignored with "()", just to change the priority ), it indicates that the pointer points to an array and then combines it with int, indicating that the elements in the array are integer. therefore, P is a pointer to an array composed of integer data.

Int ** P;// Start with P, first with *, indicating that p is a pointer, and then with *, indicating that the element pointed to by the pointer is a pointer, and then with int, the Pointer Points to an integer. so P is a pointer to the integer data.

Int P (INT );// Starting from P, it is first combined with (), indicating that p is a function and then goes to () for analysis, it indicates that the function has an integer Variable Parameter and is then combined with an external int, indicating that the return value of the function is an integer data. therefore, P is a function with an integer parameter and returns an integer type.

INT (* p) (INT );// Starting from P, it is first combined with the pointer, indicating that p is a pointer, and then combined with (), indicating that the pointer points to a function, and then () the Int combination in indicates that the function has an int-type parameter, and is combined with the outermost int, indicating that the return type of the function is integer, so P is a pointer to a function with an integer parameter and return an integer type.

Int * (* P (INT) [3];// Starting from P, it is first combined with (), indicating that p is a function and then enters (). Combining with int indicates that the function has an integer variable parameter, then it is combined with the outer *, indicating that the function returns a pointer, and then goes to the outermost layer. First, it is combined with [], indicating that the returned Pointer Points to an array, then it is combined with *, indicating that the elements in the array are pointers, and then combined with int, indicating that the Pointer Points to integer data. therefore, P is a function that takes an integer and returns a pointer variable pointing to an array composed of integer pointer variables.

 

2.Pointer Analysis

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 the four aspects of the pointer: pointer type, pointer type, pointer value, memory area pointed to by pointer, memory area occupied by pointer itself.

Pointer type: remove the pointer name in the pointer declaration statement. The rest is the pointer type.

Pointer pointing type: 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 (in the arithmetic operation of the pointer, the Type pointed to by the pointer has a great effect)

The memory area pointed to by the pointer: Starting from the memory address represented by the pointer value, the length is a memory area of sizeof (type pointed to by the pointer. (If a pointer points to a memory area, it means that the pointer value is the first address of the memory area)

Memory zone occupied by the pointer itself: the sizeof function can be used to measure the memory zone occupied by the pointer itself (on a 32-bit platform, the pointer occupies the length of 4 bytes)

 

3.Arithmetic Operations on pointers

Pointer and integer addition and subtraction: After a pointer ptrold is added (subtracted) and an integer N, the result is a new pointer ptrnew. The type of ptrnew is the same as that of ptrold, ptrnew points to the same type as ptrold. The value of ptrnew will increase (decrease) n times of sizeof (the type pointed to by ptrold) bytes than that of ptrold.

Addition and subtraction of pointers: two pointers cannot be used for addition operations, which is an illegal operation. Two pointers can be used for subtraction operations, but must be of the same type. They are generally used in arrays.

 

4.Operator&And*

* Is an indirect operator.

& The operation result of A is a pointer, And the pointer type is a plus *. The Pointer Points to the type, and the Pointer Points to the address, that is the address of.

* P's computation results are varied. In short, * P's result is what P points to. It has these features: its type is the type P points, the address it occupies is the address pointed to by P.

 

5.Relationship between arrays and pointers

The array name can be regarded as a pointer.

If an array type array [N] is declared, the array name array has two meanings:

First, it represents the entire array, and its type is type [N];

Second, it is a constant pointer. the pointer type is type * and the pointer type is type, that is, the type of the array unit, the Pointer Points to the memory zone, which is the unit 0th of the array. the pointer occupies a separate memory zone. Note that it is different from the memory zone occupied by the array unit 0th. The value of this pointer cannot be modified, that is, the expression similar to array ++ is incorrect.

 

6. Relationship between pointer and Structure Type

Suppose we have defined a struct, struct mystruct {INTA; int B; int C ;};

Define the structure object of the struct and initialize it. struct mystructss = {20, 30, 40 };

So how can we access the three member variables of SS through the pointer PTR?

The answer is: first define a pointer to the structure object SS, struct mystruct * PTR = & SS; then, use the pointing operator-> to access the structure object SS members.

PTR-> A; // or (* PTR). A. The former is recommended.

PTR-> B;

PTR-> C;

 

7. Relationship between pointers and functions

You can declare a pointer as a pointer to a function to call a function through the function pointer. Let's give an example to illustrate the following.

Int fun (char *, INT );

INT (* pfun) (char *, INT );

Pfun = fun;

Int A = (* pfun) ("abcdefg", 7 );

In this example, a pointer to the function fun is defined, and pfun is used as the form parameter of the function. The pointer expression is used as a real parameter to call the function fun.

 

8. Pointer type conversion

When we initialize a pointer or assign a value to a pointer, the left side of the value pair is a pointer, and the right side of the value pair is a pointer expression, which requires the types on both sides to be consistent, the types to be converted are the same. If they are different, forced type conversion is required. Syntax format: (type *) P;

In this way, the forced type conversion result is a new pointer. the type of the new pointer is type *, and it points to type. The address it points to is the address pointed to by the original pointer. Note that all attributes of the original pointer P are not modified.

In addition, if a function uses a pointer as a form parameter, the type must be consistent during the combination of the real parameters and form parameters in the function call statement. Otherwise, a forced conversion is required.

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.