Function pointer questions, answers, function pointer questions,

Source: Internet
Author: User

Function pointer questions, answers, function pointer questions,

In the codeStatements 1, 2, and 3And use the function pointer to call the FunctionStatement a and BWhich of the following statements is correct ???

# Include <stdio. h> int add (int a, int B) {return a + B;} int main () {int (* p) (int, int); int sum; p = add; // Statement 1 // p = & add; // Statement 2 // p = * add; // statement 3sum = p (1, 2 ); // Statement a // sum = (* p) (a, B); // statement bprintf ("% d \ n", sum); return 0 ;}



C language pointer

First, define the function pointer
A and D are defined as follows: A defines the pointer to the function pf. The parameter to which the function is directed is null and no return value.
D defines the pointer to the function pf. The function parameter is int or char (the title is int or char *) and no return value is returned.

B. D is defined as: Define the function pf. The function parameter is null, And the return type is void * (void pointer)

The definition of a single-slave function pointer can exclude BCD.

Second, pointer assignment
The function name and array name are similar in nature: the address of the first data in the array, that is, a [10], then a = & a [0]
The name of the function is the memory address of the first instruction in the function body ~~ So we do not need to use the address operator during replication!

Hope to help you.

Function pointer: A simple C program does not know why an error is reported during compilation.

Hello!
Because the returned values of filefunc and editfunc are of the void type.
It does not match the return value type int of the function pointer you defined, so you can change int to void.
# Include <stdio. h>
# Include <stdlib. h>
Void filefunc ()
{
Printf ("njksjk \ n ");
}
Void editfunc ()
{
Printf ("nsjknjk \ n ");
}
Int main ()
{
Typedef void (* funcp) (); // change void to int
Funcp p = filefunc;
P ();
P = editfunc;
P ();
System ("pause ");
Return 0;
}
Hope to help you

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.