I personally understand pointer and function pointers in C language.

Source: Internet
Author: User

I personally understand pointer and function pointers in C language.

1. function pointer

  As the name suggests, it refers to the pointer to the function. The function is the same as other pointers. the pointer variable stores the address of the function to which it points.

For example, the void type function pointer can be defined by void (* f) (parameter list), or typedef void (* F) (parameter list ),

F f. Note that the type of the function pointer must be the same as that of the function.

  The following is a simple example.

1 # include <stdio. h> 2 # include <stdlib. h> 3 typedef void (* F) (); 4 5 void print_hello () // simply define a print function with no parameters 6 {7 printf ("hello ~ \ N "); 8} 9 10 void main () 11 {12 F f1; // This is equivalent to void (* f1) (); 13 void (* f2 )(); 14 15 f1 = print_hello; 16 f2 = print_hello; 17 18 f1 (); 19 f2 (); 20 21 system ("pause"); 22}
View Code

2. pointer Functions

  A pointer function is a function that returns a pointer. For example, int * a () and char * B ()... indicate functions with integer pointers and struct pointers respectively.

The following is a simple example.

# Include <stdio. h> # include <stdlib. h> char * str () // define a function that returns a character pointer {char * a = "hello world ~ "; Return a;} void main () {char * c = str (); // you can assign a value to char * B at the same time when defining it; // You can also define it first, then assign B = str (); printf ("% s \ n", c, B); system ("pause ");}
View Code

 

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.