简体   繁体   English

“typedef void *(*Something)(unsigned int)”是什么意思

[英]What does “typedef void *(*Something)(unsigned int)” mean

I am wondering, what does below mean in.h, it has我想知道,下面是什么意思 in.h,它有

 typedef void *(*some_name)(unsigned int);

And then in.c然后在.c

some_name rt;
some_name State= 0;
unsigned int t = 1;
rt = (some_name) State(t);

It creates an alias some_name for a pointer to a function with a return type void* and a single unsigned int parameter.它为指向 function 的指针创建别名some_name ,返回类型为void*和单个 unsigned int 参数。 An example:一个例子:

typedef void *(*my_alloc_type)(unsigned int);

void *my_alloc(unsigned int size)
{
    return malloc(size);
}

int main(int argc, char *argv[])
{
    my_alloc_type allocator = my_alloc;
    void *p = allocator(100);
    free(p);
    return 0;
}

This typedef这个类型定义

typedef void *(*some_name)(unsigned int);

introduces an alias for pointer to a function of the type void *( unsigned int ) that is that has the return type void * and one parameter of the type unsigned int .为指向void *( unsigned int )类型的 function 的指针引入了一个别名,该指针具有返回类型void *和一个类型为unsigned int的参数。

As for this code snippet至于这个代码片段

some_name rt;
some_name State= 0;
unsigned int t = 1;
rt = (some_name) State(t);

then it does not make a sense.那么它没有任何意义。

typedef void *(*some_name)(unsigned int);
                ^^^^^^^^^

somename is somename

typedef void *(*some_name)(unsigned int);
              ^^         ^

a pointer一个指针

typedef void *(*some_name)(unsigned int);
                          ^^^^^^^^^^^^^^

to a function that accepts an unsigned int到接受无符号整数的 function

typedef void *(*some_name)(unsigned int);
        ^^^^^^

and returns a pointer to void并返回一个指向 void 的指针

typedef void *(*some_name)(unsigned int);
^^^^^^^

but for types.但对于类型。


So "the type somename is for functions that accept unsigned and return pointer to void"所以“类型somename用于接受无符号并返回指向 void 的指针的函数”

#include <stdlib.h>
typedef void *(*some_name)(unsigned int);
void *foo(unsigned t) {
    if (t == 0) return NULL;
    return malloc(t);
}
int main(void) {
    somename fxptr = foo;
    void *bar = fxptr(42);
    free(bar);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM