Function pointer in the C language struct, C language function pointer

Source: Internet
Author: User

Function pointer in the C language struct, C language function pointer

This article briefly describes the application of function pointers in struct, laying the foundation for a series of subsequent articles.

Address: Workshop.

Introduction

Pointers are an important part of the C language. Therefore, a deep understanding of pointers and the efficient use of pointers can allow programmers to write more sophisticated programs. Remember that the pointer is a variable pointing to the memory address. Pointers can be referenced, such as int, char, and so on ...... Common data types, such:

Int * intptr; // declare an int intval = 5 pointer to an integer value; // define an int variable intptr = & intval; // The intptr contains the intval address.

Pointers not only point to regular types, but also to functions.

Function pointer

It is not difficult to understand the content of function pointers. For more information, see C language function pointer usage.

Syntax

To declare a function pointer, use the following syntax:

Return Type(*Function pointer's variable name ) ( Parameters )

For example, declare a function pointer named func to receive two Integer Parameters and return an integer value.

int (*func)(int a , int b ) ; 

You can easily use the type definition to apply it to function pointers:

typedef int (*func)(int a , int b ) ; 
Function pointer in struct

First, we define a function pointer named Operation:

typedef int (*Operation)(int a , int b );

Then define a simple struct named STR.

Typedef struct _ str {int result; // used to store the result Operation opt; // function pointer} STR;

Now we can define two functions: Add and Multi:

// Sum a and B int Add (int a, int B) {return a + B;} // multiply a and B int Multi (int a, int B) {return a * B ;}

Now we can write the main function and point the function pointer to the correct function:

Int main (int argc, char ** argv) {STR str_obj; str_obj.opt = Add; // The function pointer variable points to the Add function str_obj. result = str_obj.opt (5, 3); printf ("the result is % d \ n", str_obj.result); str_obj.opt = Multi; // the function pointer variable points to the Multi function str_obj. result = str_obj.opt (5, 3); printf ("the result is % d \ n", str_obj.result); return 0 ;}

The running result is as follows:

  the result is 8  the result is 15 

The complete code is as follows:

# Include <stdio. h> typedef int (* Operation) (int a, int B); typedef struct _ str {int result; // to sotre the resut Operation opt; // funtion pointer} STR; // sum a and B int Add (int a, int B) {return a + B;} // multiply a and B int Multi (int a, int B) {return a * B;} int main (int argc, char ** argv) {STR str_obj; str_obj.opt = Add; // The function pointer variable points to the Add function str_obj. result = str_obj.opt (5, 3); printf ("the result is % d \ n", str_obj.result); str_obj.opt = Multi; // the function pointer variable points to the Multi function str_obj. result = str_obj.opt (5, 3); printf ("the result is % d \ n", str_obj.result); return 0 ;}
 

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.