Wierd function

M

mlt

How is it possible from main to supply MyErrorHandler wihtout arguments?



void MyErrorHandler(CGcontext context, CGerror error, void *data) {
char *progname = (char *)data;
fprintf(stderr, "%s: Error: %s\n", progname, cgGetErrorString(error));
}



void main(int argc, char *argv[])
{
....
cgSetErrorHandler(MyErrorHandler, (void *)argv[0]);
....
}
 
K

Keith Thompson

mlt said:
How is it possible from main to supply MyErrorHandler wihtout arguments?



void MyErrorHandler(CGcontext context, CGerror error, void *data) {
char *progname = (char *)data;
fprintf(stderr, "%s: Error: %s\n", progname, cgGetErrorString(error));
}



void main(int argc, char *argv[])
{
...
cgSetErrorHandler(MyErrorHandler, (void *)argv[0]);
...
}

A function name not followed by a parenthesized argument list isn't a
function call, it's a reference to the name of the function. This
calls the cgSetErrorHandler function, passing the address of the
MyErrorHandler function as an argument.

Presumably cgSetErrorHandler will save this address somewhere so it
can be used later for an indirect call.

Incidentally, main returns int, not void; the correct declaration is:

void main(int argc, char *argv[])

See the comp.lang.c FAQ, <http://www.c-faq.com/>, questions 11.12a,
11.12b, 11.14a, 11.14b, and 11.15.
 
K

Keith Thompson

Keith Thompson said:
Incidentally, main returns int, not void; the correct declaration is:

void main(int argc, char *argv[])

See the comp.lang.c FAQ, <http://www.c-faq.com/>, questions 11.12a,
11.12b, 11.14a, 11.14b, and 11.15.

Oops, that was a copy-and-paste error; sorry about that.

The correct declaration is:

int main(int argc, char *argv[])

(Or you can use "int main(void)" if you're not going to refer to the
command-line arguments, but that doesn't apply in this case.)
 
S

Stephen Sprunk

mlt said:
How is it possible from main to supply MyErrorHandler wihtout arguments?

void MyErrorHandler(CGcontext context, CGerror error, void *data) {
char *progname = (char *)data;
fprintf(stderr, "%s: Error: %s\n", progname, cgGetErrorString(error));
}

void main(int argc, char *argv[])
{
...
cgSetErrorHandler(MyErrorHandler, (void *)argv[0]);
...
}

Note the lack of parentheses after MyErrorHandler, which means it isn't
being called here and thus doesn't need arguments.

In this case, MyErrorHandler decays to a pointer-to-function, and that
pointer is an argument to the cgSetErrorHandler() function call.

Given the names and function signatures involved, this is most likely
setting up a "callback" function where, instead of your code calling the
library, you tell the library how to call yours. The syntax for this
can look pretty complicated, but the idea is simple once you understand
that you can have pointers to functions just like you can have pointers
to objects.

S
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,265
Messages
2,571,069
Members
48,771
Latest member
ElysaD

Latest Threads

Top