B
B
BrainHacker2012-10-17 22:41:59
C++ / C#
BrainHacker, 2012-10-17 22:41:59

How does the standard describe calling a function by pointer?

Hello.
There was a question concerning a call of function under the pointer. There is this code:

#include "stdio.h"

typedef void (*FUNC)();

void func()
{
    printf( "In func()\n" );
}

int main( int argc, char* argv[] )
{
    FUNC f = func;
    (*f)();
    f();
    return 0;
}

VS2010 code compiles, under Windows the program output is as follows:
In func()
In func()

Kernighan, Ritchie write that according to the standard, a function call by a pointer should be done, as in the first version.
Actually a question: without dereferencing of the pointer (as in the second variant) it is possible to call? What does the latest C standard say about this? Will GCC compile the code correctly?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Ramires, 2012-10-18
@BrainHacker

Will GCC compile the code correctly?

GCC and Clang result matched VS2010.
[email protected]:~$ gcc test.c
[email protected]:~$ ./a.out
In func()
In func()
[email protected]:~$ clang test.c
[email protected] :~$ ./a.out
In func()
In func()

A
Alexey Huseynov, 2012-10-17
@kibergus

Can. As you can see, the function name is a pointer to it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question