C language about pointer function and function pointer personal understanding

Source: Internet
Author: User

1, function pointer

  As the name implies, pointers to functions are the same as other pointers, which hold the address of the pointer to the function.

If the void type function pointer can be defined in either void (*f) (parameter list) or typedef void (*f) (argument list),

F. But be aware that the type of the function pointer is the same as the function type.

  Here's a simple example

1#include <stdio.h>2#include <stdlib.h>3typedefvoid(*F)    (); 4 5 voidPrint_hello ()//simple definition of a non-parametric print function6 {7printf"Hello ~\n");8 }9 Ten voidMain () One { AF F1;//This is equivalent to void (*F1) (); -     void(*F2) (); -  theF1 =Print_hello; -F2 =Print_hello; -  - F1 (); + F2 (); -  +System"Pause"); A}
View Code

2, pointer function

  A pointer function is a function that returns a value as a pointer. For example int *a (), char *b () ... A function that represents the return value as a shape pointer, a character pointer, respectively.

Here's a simple example

#include <stdio.h>#include<stdlib.h>Char*str ()//defines a function that returns a character pointer{    Char*a ="Hello World ~"; returnA;}voidMain () {Char*c = str ();//You can assign values at the same time as defined    Char(By//can also be defined first, after assignmentb =str (); printf ("%s\n%s\n", c,b); System ("Pause");}
View Code

C language about pointer function and function pointer personal understanding

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.