Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Should you dereference function pointers?

Name: Anonymous 2009-11-04 19:33

#include <stdio.h>

void func() {
  printf("hello world\n");
}

void (*go)() = func;

int main() {
  go();    // number 1...
  (*go)(); // or number 2
  return 0;
}


Which is correct ANSI C89? GCC accepts both, even in -ansi -pedantic; it outputs hello world twice.

Name: Anonymous 2009-11-07 14:01

Function pointers are special in C.  Any value with function type is automatically converted to the pointer to that function.

[code]
#include <stdio.h>
void func(void) { }

int main(int argc, char *argv[])
{
    if (func == *func) puts("func == *func");
    if (func == &func) puts ("func == &func");
    return 0;
}
[\code]

Compile the above.  No type errors.  Run it.  It prints both messages.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List