DAY11 Basic code--Function pointers

Source: Internet
Author: User

//

Main.m

Demo11

//

Created by Scjy on 15/10/29.

COPYRIGHT©2015 year Lizhipeng. All rights reserved.

//

#import <Foundation/Foundation.h>

typedef int (*MAXV) (int, int);//parameter name can be omitted

typedef int (*MAXV) (int x,int y), or//equivalent to the INT (*) (int x,int y) defined as MAXV

int maxValue (int x,int y) {

return x > Y? x:y;

}

int sum (int x,int y) {

return x + y;

}

int mult (int x,int y) {

return x * y;

}

Char Printfhello (char str) {

return str;

}

void Printhello0 (char str) {

printf ("%c\n", str);

}

void Printhello (char *str) {

printf ("Hello everyone, my name%s\n", str);

}

typedef int (*sun) (Char *a, char *b);

Int Sun (char *a,char *b) {

int str = strcmp (A, b);

return str;

}

int getValue (int x,int y,maxv P) {

return P (x, y);

}

int main (int argc, const char * argv[]) {

@autoreleasepool {

11th function Pointer *********************//

A function pointer defines a function that has a code area and also has a memory address.

function declaration function definition function call

printf ("%d\n", SUM (123, 456));

printf ("%p\n", &sum);

function pointer definition: INT (*) (int x, int y) = null pointer definition

Data type (* pointer variable name) (formal parameter list) initial value null

Char stra = ' h ';

Char *STRB =&stra;

printf ("This is a character:%c\n", Printfhello (' h '));

Define a function pointer that can point to the above function and invoke it through a function pointer implementation.

function pointer string

Printhello0 (' h ');

Char str0[6] = "Hello";

Char *str =str0;

Printhello ("Hello");

void (*P1) (char *str) = NULL;

P1 = Printhello;

P1 ("Hello");

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

p = sum;

printf ("%d\n", p (7,9));

The function pointer is pointing back

p = mult;

printf ("%d\n", p (5,12));

Int (*P3) (int x,int y) = NULL;

P3 = MaxValue;

printf ("%d\n", p3 (6,5));

MAXV p3 = MaxValue;

printf ("%d\n", p3 (60,5));

Char str4[6] = "he";

Char str5[6] = "Llo";

char *strr1 = STR4;

char *strr2 = STR5;

Sun P4 = Sun;

printf ("---===%d\n", P4 ("he", "Llo"));

Functions that assign values to function pointers should be consistent with the function prototypes defined by the function pointers.

Define two functions, one for the maximum, one for the sum, and the input maxvalue or sum to find the maximum value of 3,5 or

(hint, define a function pointer, point to a different function according to the input, and the last call is completed).

MAXV P5 = NULL;

Char let[10];

printf ("Input maxvalue or sum:\n");

scanf ("%s", let);

if (strcmp (Let, "maxValue") = = 0) {

P5 = MaxValue;

} else if (strcmp (Let, "sum") = = 0) {

P5 = sum;

} else {

printf ("null....\n");

return 0;

//        }

if (P5! = NULL) {

printf ("%d\n", P5 (60,5));

//        }

printf ("%d\n", P5 (60,5));

callback function callback

MAXV P6 = NULL;

P6 = mult;

int h = getValue (3, 5, p6);

printf ("%d\n", GetValue (3, 5, p6));

MAXV P7 = sum;

printf ("%d\n", P7 (7,9));

printf ("%d\n", GetValue (6, 6, p7));

printf ("%d\n", GetValue (6, 6, mult));

printf ("%d\n", GetValue (6, 6, maxValue));

Dynamic sorting

2.

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

p = sum;

int y = P (7, 9);

printf ("y =%d\n", y);

3.

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

int y = P (7, 9);

printf ("y =%d\n", y);

4.

Int (*p) (int, int) = NULL;

p = sum;

int y = P (7, 9);

printf ("y =%d\n", y);

}

return 0;

}

Day11 Base Code--function pointer

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.