首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

typedef 的几个小问题,懂得大神解释一上

2013-03-25 
typedef 的几个问题,懂得大神解释一下typedef void node_proc_fun_t(void*)typedef int node_comp_fun_t(

typedef 的几个问题,懂得大神解释一下

typedef void node_proc_fun_t(void*);
typedef int node_comp_fun_t(const void*, const void*);

这两句应如何理解。

[解决办法]
定义2个函数指针的类型。类似这样的代码。

typedef void node_proc_fun_t(void*);

void foo()
{
   cout << "hello,world" << endl;
}

int main()
{
   node_proc_fun_t fun = foo;
   fun();
}

[解决办法]
两个函数的声明,只是参数列表不同
[解决办法]

typedef void node_proc_fun_t(void*);

node_proc_fun_t p_node_proc_fun;


p_node_proc_fun 是一个函数指针。
该函数指针指向一个返回值为空并且参数为指针的函数。


#include <stdio.h>

typedef void node_proc_fun_t(void*);
typedef int node_comp_fun_t(const void*, const void*);

void node_proc_fun(int* ptrInt)
{
    printf ("%d\n", *ptrInt);
}

void node_comp_fun(const int* ptrInt , const char* ptrChar)
{
    printf ("%d, %s\n", *ptrInt, ptrChar);
}

int main(int argc, char *argv[])
{
    node_proc_fun_t *fun1 = NULL;
    node_comp_fun_t *fun2 = NULL;
    fun1 = node_proc_fun;
    fun2 = node_comp_fun;
    int i = 10;
    char *c = "This is a function.";
    fun1(&i);
    fun2(&i, c);
    return 0;
}

[解决办法]
#include <stdio.h>

typedef void node_proc_fun_t(void*);

void proc_jia(void *arg)
{
    int tmp = *((int *)arg);
printf("%d + 10 = %d\n", tmp, tmp + 10);
}

void proc_jian(void *arg)
{
    int tmp = *((int *)arg);
printf("%d - 10 = %d\n", tmp, tmp - 10);
}

void proc_cheng(void *arg)
{
    int tmp = *((int *)arg);
printf("%d * 10 = %d\n", tmp, tmp * 10);
}

void proc_chu(void *arg)
{
    int tmp = *((int *)arg);
printf("%d / 10 = %d\n", tmp, tmp / 10);
}


int main(int argc, char **argv)
{
node_proc_fun_t *process[4] = {proc_jia, proc_jian, proc_cheng, proc_chu};

    int i = 0, num = 100;
    for (i = 0; i < 4; i++)
    {
        process[i](&num);
    }
return 0;
}

热点排行
Bad Request.