English 中文(简体)
与“(f)(避免)”等母体有关的类型是什么意思? 它是功能原型吗?
原标题:What does a typedef with parenthesis like "typedef int (f)(void)" mean? Is it a function prototype?
  • 时间:2010-09-09 06:12:17
  •  标签:
  • c
  • typedef
typedef int (fc_name) (void);

www.un.org/Depts/DGACM/index_french.htm

这一点与职能联络点有何不同?

最佳回答

缩略语 意图是将其用于职能点,但在这种情况下,集团使用:

int bar(void);

fc_name* foo = bar; /* Note the * */

Update: As mentioned in the comments to Jonathan Leffler s answer, the typedef can be used to declare functions. One use could be for declaring a set of callback functions:

typedef int (callback)(int, void*);

callback onFoo;
callback onBar;
callback onBaz;
callback onQux;
问题回答

第一胎母体是多余的,相当于:

typedef int fc_name(void);

<>我不认为这样做有任何用处,尽管我可以让海合会自行抱怨。

这意味着,<代码>fc_name是功能类型中没有任何论点和回归的附加物int。 虽然你可以宣布<代码>rand(<>>>/code>功能,但使用:

fc_name rand;

您不能在职能定义中使用<代码>类型f。

职能类型:

typedef int (*fc_name)(void);

该守则表明,没有星号的类型不是功能点(涉及现已删除的替代答案):

static int function(void)
{
    return 0;
}

typedef int   fc_name1 (void);
typedef int  (fc_name2)(void);
typedef int (*fc_name3)(void);

fc_name1 x = function;
fc_name2 y = function;
fc_name3 z = function;

编辑时,gcc说:

gcc -Wextra -Wall -pedantic -c -O x.c
x.c:10:1: error: function ‘x’ is initialized like a variable
x.c:11:1: error: function ‘y’ is initialized like a variable

该守则表明,你确实可以使用<代码>fc_name *var = funcname;,如

static int function(void)
{
    return 0;
}

typedef int   fc_name1 (void);
typedef int  (fc_name2)(void);
typedef int (*fc_name3)(void);

fc_name1  x_0 = function;
fc_name1 *x_1 = function;
fc_name2  y_0 = function;    // Damn Bessel functions - and no <math.h>
fc_name2 *y_1 = function;    // Damn Bessel functions - and no <math.h>
fc_name3  z   = function;

• 采用0, y1生成海合会警报:

x.c:12:11: warning: conflicting types for built-in function ‘y0’
x.c:13:11: warning: built-in function ‘y1’ declared as non-function

此外,根据>schot:

static int function(void)
{
    return 0;
}

typedef int   fc_name1 (void);
typedef int  (fc_name2)(void);
typedef int (*fc_name3)(void);

fc_name1  x_0 = function;   // Error
fc_name1 *x_1 = function;   // x_1 is a pointer to function
fc_name1  x_2;              // Declare int x_2(void);
fc_name1 *x_3 = x_2;        // Declare x_3 initialized with x_2

fc_name2  y_0 = function;   // Damn Bessel functions - and no <math.h>
fc_name2 *y_1 = function;   // Damn Bessel functions - and no <math.h>
fc_name1  y_2;              // Declare int y_2(void);
fc_name1 *y_3 = x_2;        // Declare y_3 initialized with y_2

fc_name3  z   = function;

有趣——C黑暗角落确实是高潮。

  1 #include <stdio.h>
  2 
  3 
  4 typedef int (fc_name)(void);
  5 
  6 
  7 
  8 int test_func1 ()
  9 {
 10     printf("
 test_func1 called
");
 11 
 12     return 0;
 13 }
 14 
 15 int test_func2 (void)
 16 {
 17     printf("
 test_func2 called
");
 18     return 0;
 19 }
 20 
 21 int handler_func(fc_name *fptr)
 22 {
 23     //Call the actual function
 24     fptr();
 25 }
 26 
 27 int main(void)
 28 {
 29     fc_name  *f1, *f2;
 30 
 31     f1 = test_func1;
 32     f2 = test_func2;
 33 
 34     handler_func(f1);
 35     handler_func(f2);
 36 
 37     printf("
 test complete
");
 38 
 39     return 0;
 40 }

OUTPUT:-

 test_func1 called

 test_func2 called

 test complete

So the typedef which I questioned about, (Line # 4 here) represents a function type and is not the same as function pointer typedef. This kind of typedef does not have much significance. These are used as a style standard or simply to create obfuscation intentionally ;-)

有趣! 申报类型是申报,其类型为储存类别。

typedef int   fc_name1 (void);   
// this defines a function type called fc_name1 
// which takes no parameter and returns int

之后,你可以确定如下职能:

fc_name1 myFunc;
// this is equivalent to the next line
// int myFunc(void);

你们应该能够从C/c++标准中看到这一点!

以前我从未看到过这样的情况:edef name,但以功能<>/em”为名的括号有助于防止其作为同一名称的类似功能的宏观。 例如,<条码>isxxx在<条码>c类中的功能,h被定义为功能和宏观功能。 因此,您可以就<代码>salpha/code>发言。 但是,C Library define <代码>salpha? 与此类似:

#include <ctype.h>

int
(isalpha)(int c)
{
    return isalpha(c);
}

职能机构使用<代码>isalpha/code>作为宏观手段加以扩大,职能负责人使用该代码为t。

正确的表格是:

typedef int (*myfunc)(void);

您可以确定以下职能:

int helloword(void) {
    printf("hello, world
");
}

从而界定了这一职能的一个变点:

myfunc hw_func;
hw_func = helloworld;

职能单位:

int ret = (*hw_func)();

我们需要职能点的原因是,C语没有预先界定的职能点,而使用<条码>,避免*,即用C语言称职是非法的。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...