[C Language/C ++] pointer Summary

Source: Internet
Author: User

[This article is original to the Technical blog of Silence xuanyuan Sili .]

[This article is for reprinting. for reprinting, please indicate the source in the form of a link .]

[All the articles in this blog have been carefully organized by the bloggers. Please respect the fruits of my work .]

 

[C Language] pointer Summary

I. pointer operation
1, & and * operators have the same priority and are combined from right to left.
The single object operator is also a combination of right and left, so * p ++ and (* p) ++ are different. In addition, *, ++ has the same computing priority. For example, * s ++ and ++ act on s rather than * s.
2. Two pointers can be used for subtraction. For example, if p and q point to the elements in the same array, p-q indicates the number of elements between p and q. (Not two address values are subtracted)
3. addition and subtraction of the pointer and integer indicates the displacement of the pointer relative to the current position. For example, * (p + 1)
4. pointers can be compared under certain conditions (when pointing to the same shared object). For example, if p and q point to the same array, you can write p> q;

2. pointer variables as function parameters
After the function is executed, the value of the pointer does not change, and the content indicated by the pointer changes.
For example, void swap (* p, * q );

3. pointers and Arrays
1. pointer to an array element
Int a [10], * p;
P = & a [0]; or p = a; the address of the first element is assigned to p
In C language, the array name is the first address of the array.
* (P + I) indicates a [I];
2. pointer and one-dimensional array
The compilation system processes arrays. The compilation system always uses the array element form.
A [I] is converted to * (a + I) Before calculation.
3. pointer and multi-dimensional array
The system regards a two-dimensional array as an array for processing, that is, a [3] [4] is regarded as three one-dimensional arrays a [0], a [1], a [2], and each array is a one-dimensional array composed of four elements. For example, when referencing a [2] [3], the system regards a [2] as an array name (as shown in the preceding method for processing arrays) and converts it
* (A [2] + 3)
Convert a [2,
* (A + 2) + 3) // In the original one-dimensional array, the [2] system is converted to * (a + 2 ), note that the system only processes the data in the form.
A [I] [j] is * (a + I) + j)
A [I] [j] [k] is * (a + I) + j) + k)
Note: * (a + I) + j is the address of a [I] [j]; * (a + I) + j) + k is the address of a [I] [j] [k]. They do not represent content!
4. pointer variable pointing to a one-dimensional array composed of j Elements
For example:
Int a [3] [4];
Int (* p) [4]; // defines the pointer Variable p for a one-dimensional array with four elements
P =;
In this case, p + 1 points to a [1], and the growth of p is measured in the length of a one-dimensional array.
* (P + 2) + 3 is the address of a [2] [3], * (p + 2) + 3) is the value of a [2] [3;
Note that p brackets cannot be omitted. Otherwise, int * p [4] indicates a pointer array;
5. pointer array and pointer pointing to pointer
Pointer array, such as int * p [4];
It is often used to process several strings, making it easier to process strings;
Pointer to pointer:

Iv. pointers and functions
1. pointer to the Function
Type (* pointer variable name )()
For example, int (* funcp) (); indicates that funcp is defined as a pointer to a function whose return value is an integer;
For example, void swap (int x, int y );
Void (* p )();
P = swap;
Note that when defining a pointer to a function, you do not need to specify the parameter type. In addition, the first bracket is indispensable. Otherwise, the return value is the pointer function.
You can use the pointer variable pointing to the function as a function parameter. For example, if int process (int x, int y, int (* func) () exists, process can call different functions.
2. the return value is a pointer function;

5. String and pointer
Character array, string processing, etc.

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.