我听说过一些方法,但都没有卡住.我个人试图避免C中的复杂类型,并尝试将它们分解为组件typedef.
我现在面临着从一个所谓的"三星级程序员"维护一些遗留代码的问题,而且我很难阅读一些***代码[] [].
你如何阅读复杂的C声明?
考虑以下typedef:
typedef int (*f1)(float);
typedef f1 (*f2)(double);
typedef f2 (*f3)(int);
Run Code Online (Sandbox Code Playgroud)
f2是一个返回函数指针的函数.与...相同f3,但函数的类型,f3返回的指针是f2.如何在f3没有typedef的情况下定义?我知道typedef是更清晰,更容易理解的定义方式f3.但是,我的目的是更好地理解C语法.
我最近在读代码,发现函数指针写成:
int (*fn_pointer ( this_args ))( this_args )
Run Code Online (Sandbox Code Playgroud)
我经常会遇到这样的函数指针:
return_type (*fn_pointer ) (arguments);
Run Code Online (Sandbox Code Playgroud)
这里讨论类似的事情:
// this is a function called functionFactory which receives parameter n
// and returns a pointer to another function which receives two ints
// and it returns another int
int (*functionFactory(int n))(int, int) {
printf("Got parameter %d", n);
int (*functionPtr)(int,int) = &addInt;
return functionPtr;
}
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我有什么区别,这是如何工作的?