What are the different ways to call a function? For example, can I call a function without ()?
调用函数的不同方法是什么?例如,我可以在没有()的情况下调用函数吗?
4 个解决方案
#1
5
You can call by name:
你可以叫名字:
function_name(args);
You can call by function pointer:
可通过函数指针调用:
void (*function_pointer)(int, char *) = ...;
(*function_pointer)(3, "moo"); // classic function pointer syntax
function_pointer(3, "moo"); // alternate syntax which obscures that it's using a function pointer
No, you cannot call a function without using ()
. You can hide the ()
by using a macro but that just hides where they are; in the end you must use ()
somewhere.
不,不使用()就不能调用函数。您可以通过使用宏来隐藏(),但它只隐藏它们所在的位置;最后,你必须在某处使用()。
#2
4
You can use a macro:
你可以使用一个宏:
#define f func()
but this is not a recommended way. Your code will be very difficult to read and understand.
但这不是一种推荐的方式。您的代码将很难阅读和理解。
#3
3
In C the ()
is the function invocation syntax. You cannot call a function without it.
在C中,()是函数调用语法。没有它你就不能调用函数。
#4
1
There are several pedantic ways to invoke a function without using (). Naming the function "main" (with correct parameter & return types) is one good way. You could register it as an interrupt handler. You could trick the compiler into jumping into it by smashing the stack (not portable and not recommended, works with gcc on 64-bit x86):
有几种不用()调用函数的方法。命名函数“main”(具有正确的参数和返回类型)是一个好方法。您可以将它注册为一个中断处理程序。您可以通过粉碎堆栈(不是可移植的,也不推荐使用64位x86上的gcc)来骗编译器跳进去:
#include <stdio.h>
void foo()
{
printf("In foo\n");
}
void bar()
{
long long a;
long long *b = &a;
void (*fooptr)() = &foo;
b[2] = (long long)fooptr;
}
int main()
{
bar();
}
#1
5
You can call by name:
你可以叫名字:
function_name(args);
You can call by function pointer:
可通过函数指针调用:
void (*function_pointer)(int, char *) = ...;
(*function_pointer)(3, "moo"); // classic function pointer syntax
function_pointer(3, "moo"); // alternate syntax which obscures that it's using a function pointer
No, you cannot call a function without using ()
. You can hide the ()
by using a macro but that just hides where they are; in the end you must use ()
somewhere.
不,不使用()就不能调用函数。您可以通过使用宏来隐藏(),但它只隐藏它们所在的位置;最后,你必须在某处使用()。
#2
4
You can use a macro:
你可以使用一个宏:
#define f func()
but this is not a recommended way. Your code will be very difficult to read and understand.
但这不是一种推荐的方式。您的代码将很难阅读和理解。
#3
3
In C the ()
is the function invocation syntax. You cannot call a function without it.
在C中,()是函数调用语法。没有它你就不能调用函数。
#4
1
There are several pedantic ways to invoke a function without using (). Naming the function "main" (with correct parameter & return types) is one good way. You could register it as an interrupt handler. You could trick the compiler into jumping into it by smashing the stack (not portable and not recommended, works with gcc on 64-bit x86):
有几种不用()调用函数的方法。命名函数“main”(具有正确的参数和返回类型)是一个好方法。您可以将它注册为一个中断处理程序。您可以通过粉碎堆栈(不是可移植的,也不推荐使用64位x86上的gcc)来骗编译器跳进去:
#include <stdio.h>
void foo()
{
printf("In foo\n");
}
void bar()
{
long long a;
long long *b = &a;
void (*fooptr)() = &foo;
b[2] = (long long)fooptr;
}
int main()
{
bar();
}