Statement of complex pointers in C Language array pointer array function pointer function at a glance! C/C ++ mandatory test site for job interview (4)

Source: Internet
Author: User
Tags integer numbers

First, read the following eight questions. If you can do it without reading any materials. Congratulations! It should be okay. Because the complex combinations are all changed from the above eight types.

1. Define an integer

2. Define a pointer to the integer number

3. Define a pointer to the pointer, which is a pointer to the integer number.

4. Define an array with 10 integer numbers

5. Define an array with 10 pointers. These pointers all point to the integer number.

6. Define a pointer pointing to a 10 integer array.

7. Define a pointer to the function. The function has an integer parameter and returns an integer data.

8. Define an array with 10 pointers. Each Pointer Points to a function. This function has an integer parameter and returns an integer number.

The answer is as follows:

1, int

This is nothing to say.

2, int *,

* A indicates that A is a pointer, And the int on the left indicates the type of data that the Pointer Points. This may be nonsense, but the latter is complicated and often I don't know what it means.

3, int **

4, int A [10]

5, int * A [10]

Note that it starts from here. When * and [] appear simultaneously, the [] operation level is higher *, therefore, a [10] indicates that array A has 10 elements. * A [10] indicates that each element is a pointer. * It modifies a [10] instead of. Add int to point to Data Type

6, INT (* A) [10]

First, an array is required. The array contains 10 integers, that is, int A [10]. Then, a pointer is required to point to the array. Note that the pointer is to this array, instead of pointing to each element a [0] a [1] In the array in the above five questions... Therefore, the pointer modifies the array name a, so it is written as int (* A) [10]. Due to the parentheses, * takes precedence over.

7, INT (* f) (INT ),

Assume that the name of the function is f, and the pointer to the function is written as (* f). It is enclosed by parentheses. Remember this! The reason is summarized below. The function contains parameters of the int type, which are written as (* f) (INT). At last, the return type is int. Simply add int to the front. Return type.

8, INT (* A [10]) (INT)

It is required to define an array with 10 pointers, that is, five Question Types, * a [10], here a [0] a [1] ...... all are pointers. The pointer must be directed to a function and written as (* A [10]) (). Here, note that the indicator for measuring whether it is a function is followed by a bracket. The following requires that the function parameter be int and return Int. The answer is int (* A [10]) (INT ). the final meaning is a [0] a [1] A [2] ...... all are pointers. Each Pointer Points to a function. The parameter in the function is int and INT is returned.

 

With the foundation of the above eight questions, it is easy to understand the following. If you answer the eight questions above, you can not only correct the question, but also clearly describe the question in words. Below is a small case.

Question 1: define a pointer to the function func. A parameter in this function is a pointer to the int data and returns an int data.

Answer: Write (* func) () first. Note that brackets are required,When the function pointer declares, there must be two parentheses between the left and right.. The first parenthesis represents the name of the function pointer. Unless it is int sum (INT, INT), sum is not enclosed in parentheses. If it is a function pointer, it should be included. The second square brackets indicate the parameters. Add the int * parameter and the return type. The answer is int (* func) (int *)

Question 2: define a pointer to the function func. The function has two parameters: int * P and INT (* f) (int *), and returns an int. In combination, it is int (* func) (int * P, INT (* f) (int *). Note that * F should be included even though F is a letter. I will not explain anything else.

Question 3,INT (* func [5]) (int * P)

Meaning: complex combinations cannot escape the above eight types. It must be a function. The function parameter is a pointer to an int. The Return Value of the function is an int. The next step is (* func [5]). It indicates that func is a pointer array, and every element in it is a pointer. These pointers point to a function.

That is to say, func is an array, and every element in the array is a pointer. To be precise, (as can be seen from the (int * p) every element in the array is a function pointer. The function parameter it points to is int * P and the return type is int.

Question 4,INT (* func) [5]) (int * P)

Interpretation: first, let's look at the module (* func) [5]. As mentioned above, func indicates that func is a pointer to an array with five data elements. Array we all know int A [50]. As we mentioned above, 50 indicates the number of elements in the array, and the int on the left indicates the Data Type of the array element. As long as it is an array, be sure to tell it the type of the elements in it! We can see that (* func) [5] There is a * sign on the left, and then the brackets are placed outside. Therefore, we need to resolve this * number first. Here * indicates the array pointer func points to an array with five elements. The type of the elements in the array is pointer. Okay, jump out of brackets. There is a bracket on the right. Indicates a function. The input parameter of the function is int * P and the return type is int.

That is, func is a pointer to an array. The array has five elements. The data type of each element is a function pointer. These function pointers point to functions with int x parameters and return Int.

Question 5, INT (* func) (int * p) [5]

Description: first, the module (* func) (int * P) indicates that func is a function pointer, Which is redundant and expressed as. That is, INT (* A) [5], which means that a is a pointer to an array. As mentioned above, the array must have the number of arrays and the type of array elements. The function must have parameters and return types. For this question, combine the above, that is, func is a function pointer, which points to the function input parameter of the int * type, and the return type is a pointer to the array, the number of elements in the array to which it points is 5 and the data type is int.

So far, we will summarize:

I. Concepts of pointer arrays and array pointers

A pointer array means that all elements in an array are pointers. For example, int * A [5] has five elements in array A. Each element is a pointer to an int.

An array pointer indicates that there is an array of int A [5]. The number of elements in the array is 5 and the type is int. Now we need a pointer to point to this array, declare it as int (* A) [5]

2. Concepts of pointer functions and function pointers

The pointer function means that the return type of the function is a pointer. For example, int * F (int x). Here F is a function and its return type is a pointer to int data.

What is a function pointer? We hit the hot spot, INT (* f) (int x), then f is the function pointer,That is, the pointer to a function is called a function pointer.. This is why (* F) and () are added.

Why do we need function pointers? Because function code is part of the algorithm instruction in the program, it shares the same memory as integer data and various data. We can use char * P = "Hello! ", Printf (" % s ", P), print the string by the first address of the string. Therefore, you can call this function through the first address of the function, which is the source of the function pointer. Its usage is as follows:

Suppose there is now a function int sum (int A, int B), then we declare a function pointer, INT (* psum) (int A, int B) and then psum = sum, note that sum is not followed by parentheses. Sum indicates the first address of the function, which is the same as the string array. After the function pointer is initialized in this way, sum (3, 4) has the same effect as (* psum) (3, 4! The usage of function pointers is controversial. (* psum) (3, 4) and psum (3, 4) can both be used. For details, see http://blog.csdn.net/megaboy/article/details/482767. However, when a function pointer is declared as a pointer to a function, * And the font size of the pointer name must be enclosed in parentheses! The data type returned by the function is pointer, which is called pointer function. At this time, * is not included with the function name!

Summarize the array pointer and function pointer in one piece:

Array pointer, function pointer, * and pointer name must be enclosed in brackets!

3. When parsing complex pointer statements, we must remember the principle below:The array must tell it the number of elements and the data type. The function must have the form parameter type and the returned data type.

Previous Article: Implementation of the String concatenation function strcat
While (* P ++! = '\ 0') Where does P point to after it jumps out? C/C ++ test points required for job interview (3)
Next article: arbitrary positive hexagonal random scatter of MATLAB
Implementation ----------- communication simulation required

Top
2 step on
0
View comments

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.