Functions and pointers in C language learning notes

Source: Internet
Author: User

A pointer to a function

A function name can be interpreted as "a pointer to a function" in an expression, so, as in the experiment in Listing 2-2, a pointer to a function can be obtained by writing Func.
A pointer to a function is essentially a pointer (address), so you can assign it to a pointer type variable.
For example, there are the following function prototypes:
int func (double d);
The declaration of a variable that holds a pointer to this function is as follows:
Int (*func_p) (double);
And then write the following so that you can call Func through Func_p,
Int (*func_p) (double); Statement
Func_p = func; Assign Func to Func_p
Func_p (0.5); At this point, func_p equal to Func
Techniques for keeping "pointers to functions" in variables are often used in the following situations:
Button control memory in GUI "functions that need to be called when they are pressed"
Allocate processing based on "array of pointers to functions"

The latter's "array of pointers to functions", used as follows:
Int (*func_table[]) (double) = {
FUNC0,
FUNC1,
FUNC2,
FUNC3,
};

Func_table[i] (0.5); Call Func_table[i] 's function, parameter is 0.5
Use the above writing, do not write a very long switch case, only through the value of I can be assigned to the processing.

Oh? Don't understand why?
Indeed, like
Int (*func_p) (double); Pointer to function
And also
Int (*func_table[]) (double); An array of pointers to functions
Such statements cannot be read in the ordinary way.
The way in which this statement is interpreted is explained later.

on the confusion caused by pointers to functions

As explained above, for C, the function name in an expression can be interpreted as "a pointer to a function."
In signal processing, event-driven programs, this feature is often used in the form of callback functions.
/* If SIGSEGV (segmentation falut) occurs, the callback function Segv_handler * *
Signal (SIGSEGV, Segv_handler);
However, if the declaration of the Int func () is interpreted as "a function that returns int" based on the previously stated C language declaration rule, if the function name is in an expression and simply takes out the func, then it is not very weird to interpret "pointer to return int function". If you must use a pointer to a function, you must write &func.
For the above signal processing function, write
Signal (SIGSEGV, &segv_handler);
In this way, it can actually be implemented smoothly.
Instead, like
void (*func_p) ();
In this way, the variable func_p is declared as a pointer to a function, and can be written as a function call.
Func_p ();
However, such declarations as int func () are invoked in such a way as Func (), from the perspective of symmetry, for Void (*func_p), which must be written as
(*func_p) ();*
* In the early C language, it seems to be the only way to write ...
This can be done without problems.
Does it feel like the syntax of the C-language pointer to a function is rather confusing?
The reason for the chaos is that "the function in the expression can be interpreted as a" pointer to a function ", an ambiguous rule (is it just to keep the array consistent?) )。
To accommodate this confusion, the ANSI C standard makes the following exceptions to syntax:
Functions in an expression are automatically converted to "pointers to functions." However, when a function is an operand of an address operator & or sizeof operator, the function name in the expression cannot be changed to "pointer to a function."
The operands of the function call operator () are not "functions", but "pointers to functions".
If you use a dereference * for a pointer to a function, it becomes a function temporarily, but because it is in an expression, it is momentarily changed back to "pointer to a function."
The conclusion is that even a "pointer to a function" using the * operator is cast before the wall, because the operator at this time does not play any role.
As a result, the following statements can be executed smoothly,
(**********printf)  ("Hello, world\n"); No matter what, * just don't do it

Iii. Complex statements

In the standard library of ANSI C, there is a atexit () function. If you use this function, you can recall a specified function when the program ends normally.
The prototype of the atexit () is defined as follows:
int atexit (void (*func) (void));
1, the first focus on the identifier.
int atexit (void (*func) (void));
The expression of English is:
Atexit is
2, interpretation for the function ().
int atexit (void (*func) (void));
The expression of English is:
Atexit is function () returning
3, the function of the parameter section is more complex, so first of all to resolve this part. Again, look at the identifier first.
int atexit (void (*func) (void));
The expression of English is:
Atexit is function (Func is) returning
4, because there are parentheses, so here explains *.
int atexit (void (*func) (void));
The expression of English is:
Atexit is function (func are pointer to) returning
5, interpretation for the function (). The argument here is relatively simple, void (no parameters).
int atexit (void (*func) (void));
The expression of English is:
Atexit is function (func are pointer to function (void) returning) returning
6, interpretation type specifier void. This ends the explanation of the parameters section of the atexit.
int atexit (void (*func) (void));
The expression of English is:
Atexit is function (func are pointer to function (void) returning void) returning
7, interpreting the data type modifier int.
int atexit (void (*func) (void));
The expression of English is:
Atexit is function (func are pointer to function (void) returning void) Returning int
8. Translate into Chinese ...
Atexit is a function that returns an int (the argument is a pointer to a function that returns void without arguments).

Here is a more complicated example.
There is a signal () function in the standard library, and its prototype is declared as follows,
void (*signal (int sig, Void (*FUNC) (int))) (int);
1, the first focus on the identifier.
void (*signal (int sig, Void (*FUNC) (int))) (int);
The expression of English is:
Signal is
2, compared to *, () a higher priority, so explain this part first.
void (*signal (int sig, Void (*FUNC) (int))) (int);
The expression of English is:
Signal is function () returning
3, the explanation parameter part. Here are two parameters, the first parameter is the int sig.
void (*signal (int sig, Void (*FUNC) (int))) (int);
The expression of English is:
Signal is function (the sig is int,) returning
4, focus on another parameter.
void (*signal (int sig, Void (*FUNC) (int))) (int);
The expression of English is:
Signal is function (sig are int, Func is) returning
5, because there are parentheses, so here explains *.
void (*signal (int sig, Void (*FUNC) (int))) (int);
The expression of English is:
Signal is function (the sig is Int., Func is pointer to) returning
6. Explain () The representation function, and the parameter is int.
void (*signal (int sig, Void (*FUNC) (int))) (int);
The expression of English is:
Signal is function (the sig is int, func are pointer to function (int) returning) returning
7, interpreting data type modifier void.
void (*signal (int sig, Void (*FUNC) (int))) (int);
The expression of English is:
Signal is function (the sig is int, func are pointer to function (int) returning void) returning
8, the parameter part has explained the end. Then because there are parentheses, so here's an explanation of *.
void (*signal (int sig, Void (*FUNC) (int))) (int);
The expression of English is:
Signal is function (the sig is int, func are pointer to function (int) returning void) returning pointer to
9. Explain () The representation function, and the parameter is int.
void (*signal (int sig, Void (*FUNC) (int))) (int);
The expression of English is:
Signal is function (sig are int, func is pointer to function (int) returning void) returning pointer to function (int) Returni Ng
10, finally, add void.
void (*signal (int sig, Void (*FUNC) (int))) (int);
The expression of English is:
Signal is function (sig are int, func is pointer to function (int) returning void) returning pointer to function (int) Returni ng void
11. Translate into Chinese ...

Signal is a function that returns "a pointer to a function that returns void with an int," which has two arguments, one is int, and the other is "a pointer to a function that returns a void parameter of int."
If you can read the statement of this difficulty, I think there should be no more fear of the C statement.
The following instructions may make you feel more unhappy about C language.
Signal () is a function used to register signal processing (functions that are called when interrupts occur). The return value of this function is the previously registered function that handles the current signal interrupt.
That is, one of the parameters and the return value, they are all the same type--pointers to signal processing functions. In the general language, the same mode of expression appears two times and does not make you feel uncomfortable, but the process of interpreting the C language declaration is "a moment left to the right", therefore, the part of the return value is scattered on both sides.

At this point, the use of TypeDef can make the statement extraordinarily concise.

* * Excerpt from FreeBSD's man page/

typedef void (sig_t) (int);

sig_t signal (int sig, sig_t func);
The sig_t represents the type "pointer to a signal processing function."

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.