Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C中函数指针的澄清_C - Fatal编程技术网

C中函数指针的澄清

C中函数指针的澄清,c,C,以下代码来自示例-另请参见: 我认为void(*fn)(char*)听起来像lambda,但我知道它不是。 那么,也许这只是一个带括号的游戏,其中,void*fn(char*)是一个函数的声明,这个函数引用的是系统?但是为什么(char*)参数没有名称呢?这是允许的吗?它是指向函数的纯而简单的指针,被分配给系统调用的地址它将变量fn声明为函数指针(指向一个函数,该函数有一个char*类型的参数,并且不返回任何内容(void) 此变量使用系统的地址初始化-请参阅。如本页所述,这将需要给定的强制转换

以下代码来自示例-另请参见:

我认为
void(*fn)(char*)
听起来像lambda,但我知道它不是。
那么,也许这只是一个带括号的游戏,其中,
void*fn(char*)
是一个函数的声明,这个函数引用的是
系统
?但是为什么
(char*
)参数没有名称呢?这是允许的吗?

它是指向函数的纯而简单的指针,被分配给
系统
调用的地址

它将变量
fn
声明为函数指针(指向一个函数,该函数有一个
char*
类型的参数,并且不返回任何内容(
void

此变量使用
系统的地址初始化-请参阅。如本页所述,这将需要给定的强制转换

那么,也许这只是一个带括号的游戏,其中void*fn(char*)是一个函数的声明,我想这个函数是引用系统的

void(*fn)(char*)
不是一个函数,它是一个函数指针...
()
在这里很重要,你不能忽略它们

但是为什么参数(char*)没有名称?这是允许的吗


是的,它是允许这样做的。它的名称不是很重要,但是它的类型是。

首先,您需要声明一个指向
系统的函数指针

然后,通过将其重新定义为指向
put
,将其全部丢弃

然后,您似乎试图为
int
编制索引,但是您已经颠倒了
main
的通常命名约定,即

int main (int argc, char **argv)

这是一个指向函数的指针。如果您试图用C语言制作一个使用文本菜单的应用程序,我将使用指向函数的指针,而不是开关:

#include <stdio.h>
#include<unistd.h>

void clearScreen( const int x );
int exitMenu( void );
int mainMenu( void );
int updateSystem( void );
int installVlcFromPpa( void );
int installVlcFromSource( void );
int uninstallVLC( void );
int chooseOption( const int min, const int max );
void showMenu( const char *question, const char **options, int (**actions)( void ), const int length );
int installVLC( void );
int meniuVLC( void );
void startMenu( void );

int main( void ){
    startMenu();
    return 0;
}

void clearScreen( const int x ){
    int i = 0;
    for( ; i < x ; i++ ){
        printf( "\n" );
    }
}

int exitMenu( void ) {
    clearScreen( 100 );
    printf( "Exiting... Goodbye\n" );
    sleep( 1 );
    return 0;
}

int mainMenu( void ){
    clearScreen( 100 );
    printf( "\t\t\tMain Manu\n" );
    return 0;
}

int updateSystem( void ) {
    clearScreen( 100 );
    printf( "System update...\n" );
    sleep( 1 );
    return 1;
}

int installVlcFromPpa( void ) {
    clearScreen( 100 );
    printf("Install VLC from PPA \n");
    sleep( 1 );
    return 0;
}

int installVlcFromSource( void ) {
    clearScreen( 100 );
    printf( "Install VLC from Source \n" );
    sleep( 1 );
    return 0;
}

int uninstallVLC( void ) {
    clearScreen( 100 );
    printf( "Uninstall VLC... \n" );
    sleep( 1 );
    return 1;
}

int chooseOption( const int min, const int max ){
    int option,check;
    char c;

    do{
        printf( "Choose an Option:\t" );

        if( scanf( "%d%c", &option, &c ) == 0 || c != '\n' ){
            while( ( check = getchar() ) != 0 && check != '\n' );
            printf( "\tThe option has to be between %d and %d\n\n", min, max );
        }else if( option < min || option > max ){
            printf( "\tThe option has to be between %d and %d\n\n", min, max );
        }else{
            break;
        }
    }while( 1 );

    return option;
}

void showMenu( const char *question, const char **options, int ( **actions )( void ), const int length) {
    int choose = 0;
    int repeat = 1;
    int i;
    int ( *act )( void );

    do {
        printf( "\n\t %s \n", question );

        for( i = 0 ; i < length ; i++ ) {
            printf( "%d. %s\n", (i+1), options[i] );
        }

        choose = chooseOption( 1,length );
        printf( " \n" );

        act = actions[ choose - 1 ];
        repeat = act();

        if( choose == 3 ){
            repeat = 0;
        }
    }while( repeat == 1 );
}

int installVLC( void ) {
    clearScreen( 100 );
    const char *question = "Installing VLC from:";
    const char *options[10] = { "PPA", "Source", "Back to VLC menu" };
    int ( *actions[] )( void ) = { installVlcFromPpa, installVlcFromSource, mainMenu };

    size_t len = sizeof(actions) / sizeof (actions[0]);
    showMenu( question, options, actions, (int)len );
    return 1;
}

int meniuVLC( void ) {
    clearScreen( 100 );
    const char *question = "VLC Options";
    const char *options[10] = { "Install VLC.", "Uninstall VLC.", "Back to Menu." };
    int ( *actions[] )( void ) = { installVLC, uninstallVLC, mainMenu };

    size_t len = sizeof(actions) / sizeof (actions[0]);
    showMenu( question, options, actions, (int)len );

    return 1;
}

void startMenu( void ){
    clearScreen( 100 );
    const char *question = "Choose a Menu:";
    const char *options[10] = { "Update system.", "Install VLC", "Quit" };
    int ( *actions[] )( void ) = { updateSystem, meniuVLC, exitMenu };

    size_t len = sizeof(actions) / sizeof (actions[0]);

    showMenu( question, options, actions, (int)len );
}
#包括
#包括
无效清除屏幕(常数int x);
int EXITMANU(无效);
int主菜单(无效);
int更新系统(void);
int installVlcFromPpa(无效);
int installVlcFromSource(无效);
int-VLC(无效);
int-chooseOption(常量int-min,常量int-max);
void showMenu(常量字符*问题,常量字符**选项,整数(**动作)(void),常量整数长度);
int installVLC(无效);
国际货币基金组织(无效);
作废开始菜单(作废);
内部主(空){
开始菜单();
返回0;
}
无效清除屏幕(常数int x){
int i=0;
对于(;i最大值){
printf(“\t该选项必须介于%d和%d之间\n\n”,最小值和最大值);
}否则{
打破
}
}而(1),;
返回选项;
}
void showMenu(常量字符*问题,常量字符**选项,整数(**动作)(void),常量整数长度){
int=0;
int repeat=1;
int i;
int(*法案)(无效);
做{
printf(“\n\t%s\n”,问题);
对于(i=0;i

编译并试用。

这一行是在声明函数指针的一个变量,该变量接受一个参数,但不返回任何内容(void),这与类型转换一起需要到同一个函数原型,因为外部函数的类型类似于void*指针,它们在编译期间不会提前绑定

void (*fn)(char*)=(void(*)(char*))&system;
该行获取错误声明的符号
system
(应该是
int(const char*)
,而不是隐式
int
),将其转换为
fn
类型,并初始化新的局部变量。
类型是<
#include <stdio.h>
#include<unistd.h>

void clearScreen( const int x );
int exitMenu( void );
int mainMenu( void );
int updateSystem( void );
int installVlcFromPpa( void );
int installVlcFromSource( void );
int uninstallVLC( void );
int chooseOption( const int min, const int max );
void showMenu( const char *question, const char **options, int (**actions)( void ), const int length );
int installVLC( void );
int meniuVLC( void );
void startMenu( void );

int main( void ){
    startMenu();
    return 0;
}

void clearScreen( const int x ){
    int i = 0;
    for( ; i < x ; i++ ){
        printf( "\n" );
    }
}

int exitMenu( void ) {
    clearScreen( 100 );
    printf( "Exiting... Goodbye\n" );
    sleep( 1 );
    return 0;
}

int mainMenu( void ){
    clearScreen( 100 );
    printf( "\t\t\tMain Manu\n" );
    return 0;
}

int updateSystem( void ) {
    clearScreen( 100 );
    printf( "System update...\n" );
    sleep( 1 );
    return 1;
}

int installVlcFromPpa( void ) {
    clearScreen( 100 );
    printf("Install VLC from PPA \n");
    sleep( 1 );
    return 0;
}

int installVlcFromSource( void ) {
    clearScreen( 100 );
    printf( "Install VLC from Source \n" );
    sleep( 1 );
    return 0;
}

int uninstallVLC( void ) {
    clearScreen( 100 );
    printf( "Uninstall VLC... \n" );
    sleep( 1 );
    return 1;
}

int chooseOption( const int min, const int max ){
    int option,check;
    char c;

    do{
        printf( "Choose an Option:\t" );

        if( scanf( "%d%c", &option, &c ) == 0 || c != '\n' ){
            while( ( check = getchar() ) != 0 && check != '\n' );
            printf( "\tThe option has to be between %d and %d\n\n", min, max );
        }else if( option < min || option > max ){
            printf( "\tThe option has to be between %d and %d\n\n", min, max );
        }else{
            break;
        }
    }while( 1 );

    return option;
}

void showMenu( const char *question, const char **options, int ( **actions )( void ), const int length) {
    int choose = 0;
    int repeat = 1;
    int i;
    int ( *act )( void );

    do {
        printf( "\n\t %s \n", question );

        for( i = 0 ; i < length ; i++ ) {
            printf( "%d. %s\n", (i+1), options[i] );
        }

        choose = chooseOption( 1,length );
        printf( " \n" );

        act = actions[ choose - 1 ];
        repeat = act();

        if( choose == 3 ){
            repeat = 0;
        }
    }while( repeat == 1 );
}

int installVLC( void ) {
    clearScreen( 100 );
    const char *question = "Installing VLC from:";
    const char *options[10] = { "PPA", "Source", "Back to VLC menu" };
    int ( *actions[] )( void ) = { installVlcFromPpa, installVlcFromSource, mainMenu };

    size_t len = sizeof(actions) / sizeof (actions[0]);
    showMenu( question, options, actions, (int)len );
    return 1;
}

int meniuVLC( void ) {
    clearScreen( 100 );
    const char *question = "VLC Options";
    const char *options[10] = { "Install VLC.", "Uninstall VLC.", "Back to Menu." };
    int ( *actions[] )( void ) = { installVLC, uninstallVLC, mainMenu };

    size_t len = sizeof(actions) / sizeof (actions[0]);
    showMenu( question, options, actions, (int)len );

    return 1;
}

void startMenu( void ){
    clearScreen( 100 );
    const char *question = "Choose a Menu:";
    const char *options[10] = { "Update system.", "Install VLC", "Quit" };
    int ( *actions[] )( void ) = { updateSystem, meniuVLC, exitMenu };

    size_t len = sizeof(actions) / sizeof (actions[0]);

    showMenu( question, options, actions, (int)len );
}
void (*fn)(char*)=(void(*)(char*))&system;
int main(int argv,char **argc)
extern system,puts;
void (*fn)(char*)=(void(*)(char*))&system; // <==
fn=(void(*)(char*))&puts;
strcpy(buf,argc[1]);