C language complex variable Declaration (array, pointer, function)

Source: Internet
Author: User

I recently reviewed the C language pointer information and summarized the complex statements in C language.

The complex declaration here refers to interpreting the meaning of the declared code like the following.

int (*(*x)(int *,char *))(int);

(The code above declares a function pointer. This function receives an integer pointer and a character pointer as the parameter and function pointer. This function receives an integer parameter and returns an integer .)

The C language variable statement always implements two points:1. The declaration and the syntax used should be consistent as much as possible.

For example, declare a function pointer.

double (*fun)(double);

Use this function pointer

#include <math.h>fun=sin;doube reslut=(*fun)(0.5);//use point fun
2. The declaration statement is not based on the reading order from left to right, but on the priority of each symbol.

This is very important. It is used to determine whether a declaration declares a variable. There are three types of delimiters:

* () []

"*" Declares pointers and various pointers, including function pointers;

"()" Is specifically used to declare function pointers;

"[]" Is used to declare arrays.

The three operators "[]" and "()" have the highest priority. The combination law is "from right to left ".

After clarifying these two points, we can use these two points to interpret complex statements.

Specific rules: first, read the declared variable according to the priority and determine the variable to be declared. Next, one layer is split outward step by step. * Indicates the pointer; [] indicates the array; () indicates the function.

Example:

int (*(*x)(int *,char *))(int);

First, find X, and then find * X based on the priority. This proves that the entire statement declares a pointer. Then we can see that the right side of (* X) is a () character, which proves that X is a pointer to the function, since it is a function pointer, the rest is to describe the function return value type and parameter type. Then, you can see that the function input is an integer pointer and a character pointer. Then, take "(* X) (int *, char *)" as the whole, and find the * symbol with the highest priority to prove that the returned value is a pointer; then, if you find (INT), the pointer points to a function. The parameter of the function is an integer. Then, you can find the last Int, the returned value of this function is an integer. The explanation is complete.

Simply put, a function pointer is declared. This function receives an integer pointer and a character pointer as a parameter and returns a function pointer. The function pointing to this function receives an integer parameter, returns an integer.

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.