什么时候使用任何语言的指针要求有人使用多个指针,让我们说一个三指针.什么时候使用三指针而不是只使用常规指针是有意义的?
例如:
char * * *ptr;
Run Code Online (Sandbox Code Playgroud)
代替
char *ptr;
Run Code Online (Sandbox Code Playgroud) 比方说,我有一系列具有相同原型的功能
int func1(int a, int b) {
// ...
}
int func2(int a, int b) {
// ...
}
// ...
Run Code Online (Sandbox Code Playgroud)
现在,我想简化他们的定义和声明.当然我可以使用这样的宏:
#define SP_FUNC(name) int name(int a, int b)
Run Code Online (Sandbox Code Playgroud)
但是我想把它保存在C中,所以我尝试使用存储说明符typedef:
typedef int SpFunc(int a, int b);
Run Code Online (Sandbox Code Playgroud)
这似乎适用于声明:
SpFunc func1; // compiles
Run Code Online (Sandbox Code Playgroud)
但不是定义:
SpFunc func1 {
// ...
}
Run Code Online (Sandbox Code Playgroud)
这给了我以下错误:
error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
Run Code Online (Sandbox Code Playgroud)
有没有办法正确地做到这一点还是不可能?根据我对C的理解,这应该有效,但事实并非如此.为什么?
注意,gcc理解我要做的事情,因为,如果我写的话
SpFunc func1 = { /* ... */ }
Run Code Online (Sandbox Code Playgroud)
它告诉我
error: function 'func1' is initialized like a …Run Code Online (Sandbox Code Playgroud) 可能重复:
如何在C中将函数作为参数传递?
假设我有一个叫做的函数
void funct2(int a) {
}
void funct(int a, (void)(*funct2)(int a)) {
;
}
Run Code Online (Sandbox Code Playgroud)
调用此函数的正确方法是什么?我需要设置什么才能让它工作?
假设我正在创建一个国际象棋程序.我有一个功能
void foreachMove( void (*action)(chess_move*), chess_game* game);
Run Code Online (Sandbox Code Playgroud)
这将在每个有效的移动中调用函数指针动作.这一切都很好,但是如果我需要将更多参数传递给动作函数呢?例如:
chess_move getNextMove(chess_game* game, int depth){
//for each valid move, determine how good the move is
foreachMove(moveHandler, game);
}
void moveHandler(chess_move* move){
//uh oh, now I need the variables "game" and "depth" from the above function
}
Run Code Online (Sandbox Code Playgroud)
重新定义函数指针不是最佳解决方案.foreachMove函数是通用的,代码中的许多不同的地方都引用它.对于每个引用都必须更新它们的函数以包含它们不需要的参数是没有意义的.
如何将额外的参数传递给我通过指针调用的函数?
在我的开源纯C代码中,我使用这个简单的结构来读取和解析字符串缓冲区中的数据:
typedef struct lts_LoadState
{
const unsigned char * pos;
size_t unread;
} lts_LoadState;
Run Code Online (Sandbox Code Playgroud)
使用这个简单的API访问缓冲区:
/* Initialize buffer */
void ltsLS_init(lts_LoadState * ls,const unsigned char * data, size_t len);
/* Do we have something to read? (Actually a macro.) */
BOOL ltsLS_good(ls);
/* How much do we have to read? (Actually a macro.) */
size_t ltsLS_unread(ls);
/* Eat given number of characters, return pointer to beginning of eaten data */
const unsigned char * ltsLS_eat(lts_LoadState * …Run Code Online (Sandbox Code Playgroud) 我只是注意到这个编译没有任何错误或警告使用-pedantic -Wall两者gcc和clang.
#include <stdio.h>
int x = 0;
void func(int f(const char *)) {
f("func()!");
}
int main(void) {
func(puts);
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,似乎将参数f视为指向函数的指针int (*)(const char *).
但这种行为是我从未见过或听过的.这是合法的C代码吗?如果是这样,那么当您将函数作为函数的参数时会发生什么?
有没有办法在C++ 中将函数作为参数传递,比如函数在C中作为参数传递的方式?我知道可以使用函数指针将函数作为参数传递给C,我想知道在C++中是否可以实现相同的功能.
我想将函数作为参数传递给另一个函数.我已经在谷歌上搜索过有关这方面的信息,我发现已经有了解释,但它对我不起作用,我不知道为什么.
我有以下代码:
void doSomething(uint64_t *);
Run Code Online (Sandbox Code Playgroud)
这是我想通过的功能.
int functionToCall(int x, int y, void (*f)(uint64_t *));
Run Code Online (Sandbox Code Playgroud)
这是我要调用的函数并传递doSomething()函数.
我的代码现在是:
uint64_t *state = malloc(sizeof(uint64_t) * 10);
void (*f)(uint64_t *) = doSomething;
functionToCall(2, 3, f(state));
Run Code Online (Sandbox Code Playgroud)
如果我现在编译上面的代码,我总是得到:
错误:无效使用void表达式
有什么问题?
我正在分析一个C程序,我发现这里有一个奇怪的函数调用函数定义:
static void endSignal (int32_t dummy)
{
if (nTerminating) return;
nTerminating=1;
printf("terminating....\n");
terminateDLNAsystem();
sleep(1);
exit (0);
}
Run Code Online (Sandbox Code Playgroud)
该函数采用int32_t参数!现在这个调用"endSignal"的主函数
int32_t main (int32_t argc, char **argv)
{
/*Statements
.
.
*/
signal(SIGINT, endSignal);
signal(SIGABRT, endSignal);
signal(SIGQUIT, endSignal);
signal(SIGTERM, endSignal);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
主函数调用endSignal没有任何参数,在这种情况下会发生什么?
c ×7
pointers ×4
c++ ×3
function ×3
parameters ×3
architecture ×1
duck-typing ×1
func ×1
typedef ×1