473,512 Members | 15,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Wierd function

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]);
....
}
Oct 13 '08 #1
5 1680
mlt wrote:
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[])
That should be `int main', and you will be punished for this
departure from the ways of righteousness. Cardinal Fang, poke
him with the soft cushions!
{
...
cgSetErrorHandler(MyErrorHandler, (void *)argv[0]);
This is *not* a call to the MyErrorHandler function. It is
a call to the cgSetErrorHandler function, passing two arguments.
The first argument is a pointer to the MyErrorHandler function,
and the second is a pointer to the program's first command-line
argument (ordinarily the program name), converted from a `char*'
to a `void*'.

There are quite a few pieces missing from the snippet you've
posted, but I'd guess that the scenario goes something like this:
cgSetErrorHandler stores its two arguments somewhere and returns,
probably without doing much of anything else. Later on, if the
CG framework detects an error of some kind, the error-processing
code will retrieve the values that were stored earlier and will
use them to call the pointed-to function -- in this case, to call
MyErrorHandler. The MyErrorHandler function appears to take
three arguments: A CGcontext and a CGerror, which presumably give
information about the error, and a `void*' which is most likely
the other value stashed by cgErrorHandler.

This technique is often referred to as using a "callback,"
meaning that the invoker provides a function pointer to a called
function or suite of functions, which then uses the pointer to
"call back" to the function the invoker provided. The Standard C
library includes a few uses of this technique: the qsort() and
bsearch() functions are often cited as uses of callback, but the
way atexit() "registers" functions to be called during exit()
processing seems a closer match to the situation you've shown.

--
Er*********@sun.com
Oct 13 '08 #2
"mlt" <as**@asd.comwrites:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 13 '08 #3
On 13 Oct 2008 at 19:31, Keith Thompson wrote:
the correct declaration is:

void main(int argc, char *argv[])
There it is, right from the horse's mouth! Worthy of forming someone's
signature...

Oct 13 '08 #4
Keith Thompson <ks***@mib.orgwrites:
[...]
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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 13 '08 #5
mlt wrote:
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
Oct 13 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2851
by: Markus Fischer | last post by:
Hi, I'm experiencing a wierd problem with IE 6 in Windows with two _slightly_ different Version. Give the following HTMl-Code, ideally the output of offsetTop should be "105"; a few pixel...
3
1400
by: Sathyaish | last post by:
A practice excercise from K&R. Kindly read the comments within the program. I'd be very grateful to people who helped. Why is it that I get the wierd face-like characters on the screen instead of...
2
1842
by: Vaddina Prakash Rao | last post by:
Hi all .. I describe here a wierd behaviour .. i dont understand why ... This could be very stupid aswell .. so please bear me .. I have been writing a program to accept multiple parameters and...
1
6800
by: paul reed | last post by:
Hello, I am having some weird behavior between two machines...one which is running the 1.1 framework and one which is running 1.0. After opening a child form from a parent...I update the...
0
1096
by: Michael | last post by:
Hi, I found a wierd problem in DataGrid. If I set DataGrid's DataSource to empDataSet1 at designtime, then I can never change its DataSource at runtime, e.g., in the Button1_Click event...
1
935
by: Shaman | last post by:
ok i got this wierd problem which took me 4 hours already and im still stuck :/ anybody know what is wrong here ------Code----- Public Class guildnod Private node() As TreeNod Private tot As...
0
1169
by: Tom | last post by:
OK, here's a wierd one... I have a listbox, which I fill with strings (in my case, file names). I normally load this via a loop, adding each item to the list box in the loop. I put lb.BeginUpdate...
1
1537
by: richardsayre | last post by:
I am making a spell checker. The reqest sends out the text and gets back HTML of the suggestions and misspelled words. When you click on a misspelled word the suggestion list pops up and you can...
3
1528
by: Tom | last post by:
We are experiencing some wierd debugging behavior. What happens is that, during debugging with VS 2003, the debugger seems to 'skip' statements that are associated with database operations. For...
5
1706
by: desktop | last post by:
I am confused about the use of the template parameter "E" in the below class. Since when is it allowed to use these parameters like "E(1)" and what does it mean (where can I read more about this...
0
7252
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7093
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5676
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5077
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.