Call a function using a function name string [C Language]

Source: Internet
Author: User
Tags define function
Problem Introduction

In C, the function is defined first and then used. A simple example

/*************************   add by oscar999************************/func1(){    printf("this is func1!\n");}void main(){    func1();}

Defines a simple function func1, which can be called only by "function name. (For how to handle the call process, you must refer to the Assembly knowledge .)

However, this situation may occur during the actual development process. The function name is not determined in advance and saved by a string variable. How can this situation be called?

For a more specific example, the function name is entered through external input.

/*************************   add by oscar999************************/void func1(){    printf("this is func1!\n");}void main(){    char c[20] = "\0";    printf("please input func name:");    scanf("%s",c);  //input func1    //how run func1 ???}

How to execute func1?

Function pointer

Before solving the above problems, we should first introduce the function pointer concept.

In C, the memory address of a Data variable can be stored in the corresponding pointer variable, such as int * and char,

The same applies to functions. The first address of a function can also be stored in a function pointer variable. With this function pointer variable, we can call the function pointed.

How to define function pointer variables? Very simple.

Corresponding to the above func1 function, you can define the following pointer variables.

Void (* funcp )()

You can use the funcp name as needed. The following example calls a function using a function pointer.

/*************************   add by oscar999************************/func1(){    printf("this is func1!\n");}void main(){    void (*funcP)();    funcP = &func1;    (*funcP)();}

Use a function pointer to call a function by using a function name string

With the above example, it is not difficult to think:

If we define some function pointers in advance and get the corresponding function pointer through the input function name string, we can call the function.

/*************************   add by oscar999************************/     #include <stdio.h>typedef void (*funcP)();void func1(){    printf("this is func1!\n");}void func2(){    printf("this is func2!\n");}funcP getFuncPointer(char* sfuncname){    if(strcmp(sfuncname,"func1")==0)    {         return &func1;    }else if(strcmp(sfuncname,"func2")==0){         return &func2;    }    return NULL;                                                                                                  }void main(){    char c[20] = "\0";    funcP funcp = NULL;    printf("please input func name:");    scanf("%s",c);  //input func1 or func2    funcp = getFuncPointer(c);     if(funcp!=NULL)    {       (*funcp)();    }}

You may wonder, isn't it okay to directly call the corresponding function through the input string? For example:

/*************************   add by oscar999************************/     #include <stdio.h>typedef void (*funcP)();void func1(){    printf("this is func1!\n");}void func2(){    printf("this is func2!\n");}void main(){    char c[20] = "\0";    funcP funcp = NULL;    printf("please input func name:");    scanf("%s",c);  //input func1 or func2    if(strcmp(c,"func1")==0)    {         func1();    }else if(strcmp(c,"func2")==0){         func2();    }}

These functions can be used in less cases. However, if there are many func functions, or if this part is generated dynamically, the pointer function method is quite good.

The actual designer can put the function name string and function pointer in a name value set, so that the function pointer can be easily obtained.

Status with Parameters

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.