The relationship between the C language pointer and the function

Source: Internet
Author: User

One:

function and pointer relationships:

1) Pointer to function

2) pointers as arguments to functions

int max (int *p1,int *p2) {

}

1) An argument can be a pointer

2) can also be an array name of an array of the same type as the formal parameter

3) The return value is a function of the pointer:

Pointer functions

char* function name () {

return address

}

The return value is a string pointer

char * Test () {

return "XXXX";

}

char * TEST2 () {

Char ch= ' a ';

Return &ch;

}

Two: pointers to functions:

1) Pointer to function

function in memory will be divided into a separate area of memory, function name represents the memory of the functions of the first address

Represents the first memory address: Array name variable name Function name structure body name

2) Function Pointers:

Pointer variable to hold the address of the function

Format:

int max (int x,int y) {

Return x>y?x:y;

}

int sum (int x,int y) {

return x+y;

}

int * Getmax (int x,int y) {

Return x>y?&x:&y;

}

The format of the Declaration

int max (int x,int y);

To eject a function pointer by declaring

return value type (* pointer variable name) (parameter of function);

Defines a function pointer variable p,p can point to all return values that are int,

Functions with two shaping parameters

Int (*p) (int x,int y);

Note: When defining a function pointer, you can not specify the shape parametric name, but the type cannot be omitted

Int (*P1) (int,int);

3) Use of function pointers

(1) class to function pointer

Int (*p) (int x,int y);

Initialize the function pointer with the name of the function

p = sum;

(2) Calling functions with pointer variables

P (4,5);

Pros: Make function calls more flexible

The pointer to the block in OC is very similar to the function pointer

The relationship between the C language pointer and the 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.