2008年12月14日 星期日

函式指標 II

/*---------------
int ( *WhatAmI( int, int( * ) (float)) ) ( float );
WhatAmI( int, int( * ) (float)) 傳入一個 int 型態 與 一個 函式指標
回傳一個 int * ( float )函式指標
----------------*/
ref:工程師的家的C之練功房

範例一:
#include <stdio.h>
#include <stdlib.h>

int ( *WhatAmI( int, int( * ) (float)) )(void);
int test(float a); // int( * ) (float)

int kk(void)
{
    return 321;
}

int ( *WhatAmI( int a, int( *testA ) (float ff)) )(void)
{

    printf("%d\n",a);
    
    float ff = 111.12;
    int ret=0;
    ret=testA(ff); 
    printf("ret=%d\n", ret);
    
    return kk;
}


int test(float ff)
{
    printf("Hi, %2f\n",ff);
    return 123;
}

int main(void)
{
    int a = 100;
    int (*k)(void);

    k=WhatAmI(a,test);
    printf("k=%d\n", k());

    getchar();
    
    return 0;
}
(感謝Jason範例)
#include <stdio.h>
#include <stdlib.h>

int ( *WhatAmI( int, int( * ) (float)) ) ( float );
int test(float a); // int( * ) (float)

int main(void)
{
    int a = 100;
    int (*k)(float);
    int b=0;

    k=WhatAmI(a,test);
    b=k(31.33);
    printf("b=%d\n",b);

    getchar();
    
    return 0;
}

int ( *WhatAmI( int a, int( *test ) (float ff)) ) ( float )
{

    printf("%d\n",a);
    
    float ff = 100.12;
    
    test(ff); 
    
    return test;
}

int test(float ff)
{
    printf("%f\n",ff);
    return 123;
}

沒有留言:

張貼留言