问题描述
我是C语言的新手,但是这种语法是什么意思?
typedef Value (*NativeFn)(int argCount,Value* args);
据我了解,此处使用“值”来定义新名称的类型。
我不了解的部分是(*NativeFn)(int argCount,Value* args);
,这部分是什么意思?
解决方法
其他人正确地说了这一点:
typedef Value (*NativeFn)(int argCount,Value* args);
为函数指针类型创建typedef名称NativeFn
。
typedef
的语法与一般的C声明的语法一样,可能会造成混淆。 typedef
功能实际上是在建立声明语法后添加到语言中的,并且必须在不破坏其他任何内容的情况下进行添加。解决方案是在语法上将typedef
视为存储类说明符(尽管在语义上不是一个)。 typedef
以外的存储类指定符是extern
,static
,_Thread_local
,auto
和register
。
这意味着您可以通过将关键字typedef
替换为typedef
来理解static
声明。如果static
声明声明某种类型的对象(或函数),则相应的typedef
声明将创建具有相同名称和类型的类型定义。所以这个:
static int foo;
创建类型为foo
(具有静态存储持续时间)的对象int
,
typedef int foo;
创建类型名称foo
,它是类型int
的别名。
因此,如果您的声明是:
static Value (*NativeFn)(int argCount,Value* args);
它将已将NativeFn
定义为指向函数的对象(函数返回类型为Value
的结果)。用static
替换typedef
意味着NativeFn
是一个类型名称,它引用相同的指向函数的指针类型。
记住typedef
不会创建新类型也很重要。它会为现有类型创建一个新名称。
考虑这样的记录
Value (int argCount,Value* args)
它表示具有返回类型Value
和两个参数int
和Value *
的函数类型。
标识符Value
在其他地方声明,例如可以是类型的别名。
要为您可以编写的函数类型引入指针类型的别名
typedef Value (*NativeFn)(int argCount,Value* args);
因此,如果您具有例如这样的功能
Value some_function(int argCount,Value* args);
然后您可以通过以下方式使用typedef别名定义声明此函数的指针
NativeFn pointer_to_some_function = some_function;
,
好吧
typedef (*NativeFn)(int argCount,Value* args)
表示
NativeFn
是一个function type(can also be called "a pointer to function" or "function pointer")
,它返回Value
,并以int
和Value *
作为参数。
如果您很难理解为什么以及如何使用它,那么请阅读下面的代码,特别是注释,您会清楚(*NativeFn)(int argCount,Value* args)
的含义和用法:>
#include <stdio.h>
// note: "Value" is a type declared earlier
// for discussions sake we're declaring our own Value type
typedef struct __value {
int x,y;
} Value;
// now,the following tells us that:
// "NativeFn" is some function type that returns "Value"
typedef Value (*NativeFn)(int argCount,Value* args);
// okay,see how to use "NativeFn"
// for use "NativeFn" we've to declare some function first
// which takes two arguments same as "NativeFn" and return "Value"
Value someFun(int argCount,Value* args) {
// do something argCount and args
// at last it should return some "Value" type
Value v = {2,3};
return v;
}
int main() {
// now its time to use "NativeFn"
NativeFn fun;
fun = someFun; // notice we can use fun as a variable and assign a
// function to it,which must take arguments same as "NativeFn" and returns "Value" type
Value input = {10,12};
Value output = fun(1,&input); // note,we're calling "fun",not "someFun"
// it'll output 2,3,cause we're returning Value v = {2,3} from "someFun"
printf("(x,y): %d,%d\n",output.x,output.y);
return 0;
}
如果您有任何问题,请在评论中问我...
, typedef
工具用于为类型创建别名。例如,
typedef int *iptr;
创建名称iptr
作为类型int *
的同义词,因此您可以使用来声明指针
iptr p,q;
代替写作
int *p,*q;
C声明语法比大多数人意识到的要复杂一些,尤其是在涉及指针的地方。基本规则是:
T *p; // p is a pointer to T
T *a[N]; // a is an array of pointer to T
T *f(); // f is a function returning pointer to T
T (*a)[N]; // a is a pointer to an array of T
T (*f)(); // f is a pointer to a function returning T
T const *p; // p is a pointer to const T
const T *p; // same as above
T * const p; // p is a const pointer to T
事情可能会变得非常复杂-您可以拥有指向函数的指针数组:
T (*a[N])();
或返回数组指针的函数:
T (*f())[N];
甚至更残暴的暴行。当混合中有参数时,函数指针的声明会更难看;幸运的是,您只需要在声明中列出参数 types 而不是名称:
typedef Value (*NativeFn)(int,Value*);
使事情变得更简单。
该声明创建NativeFn
作为类型“要使用int
和Value *
并返回Value
的函数指针”的同义词。
假设您将函数定义为
Value foo( int argcCount,Value *args )
{
Value result;
...
return result;
}
如果要创建一个指向此函数的指针,名为fptr
,通常将其声明为
Value (*fptr)(int,Value *) = foo;
但是,上面的typedef
声明允许您编写
NativeFn fptr = foo;
话虽如此,请typedef
保留使用。问题在于,尽管它可以创建易于阅读的声明某些项目的方式,但它也隐藏了一些潜在有用的信息。例如,尽管上面的iptr
示例,最好不要将指针隐藏在typedef后面-如果有人需要在*
或{上使用一元p
运算符{1}}为了正确使用它们,则该信息需要包含在这些项目的声明中。如果有人需要调用q
所指的东西,那么他们需要知道返回类型是什么,参数的数量和类型等,但是所有这些信息都将丢失在使用typedef名称的声明中。