tags:

views:

106

answers:

4

For example,
Can I call a function without ()?

Thanks in advance.

+3  A: 

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.

Maurizio Reginelli
this is pure evil.
Tesserex
@Tesserex Oh yeah!
Iulian Şerbănoiu
Yep, try declaring variable names `f` now :)
Nikolai N Fetissov
Thanks, you understand my question, great answer, but is not the idea that I wanted...thanks again.
drigoSkalWalker
+5  A: 

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.

R Samuel Klatchko
+3  A: 

In C the () is the function invocation syntax. You cannot call a function without it.

Nikolai N Fetissov
+1 nothing to comment/add
Iulian Şerbănoiu
+1  A: 

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):

#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();
}
please, comment your code! I didn't understand it...
drigoSkalWalker