Kraken
Kraken

Reputation: 24233

When to know if a function is a callback?

I took the code Hello World In GTK from this link. There it uses a number of callback functions but for each callback function say

static void hello( GtkWidget *widget,gpointer data )

When It uses it as a callback it does

g_signal_connect (button, "clicked",G_CALLBACK (hello), NULL);

But here, it does not pass any arguments to hello() function. If I try to do this in a normal function, i.e.

#include <stdio.h>
int hello(int a) {
   printf("hello");
}

void main() {
   int j=10;
   hello;
}

it does not give me any error and no output too.

Q1. Howcome callback functions ignore the arguments?

Q2. How to make a function callback? i.e. in my second hello example, will the function hello be callback?

Upvotes: 0

Views: 715

Answers (3)

sfstewman
sfstewman

Reputation: 5677

What you're doing is attaching a function to a signal by passing a pointer to the function to GLib. The point of this is to call the function when the signal is triggered. In this case, that is whenever the GTK button is pressed. Basically, this links a user action (push the button) to your function, but does it in a way where the code that implements the GTK button never has to know about your function.

The macro G_CALLBACK casts a function pointer to the type GCallback, which is defined:

typedef void (*GCallback)(void);

As the documentation explains:

The type used for callback functions in structure definitions and function signatures. This doesn't mean that all callback functions must take no parameters and return void. The required signature of a callback function is determined by the context in which is used (e.g. the signal to which it is connected). Use G_CALLBACK() to cast the callback function to a GCallback.

The reason that GLib does this is to make the callback type uniform (and document what you're doing with the macro). GLib allows callbacks that take different numbers of parameters, but provides a common, consistent interface for attaching these callbacks to a signal. Internally, GLib dispatches the function with the correct number of parameters.

To understand more about function pointers, see this SO question: What is the point of function pointers?

Upvotes: 1

user1952500
user1952500

Reputation: 6781

The code compiles because saying hello is just like mentioning a int value. For example:

hello; // this just writes the function pointer with a ; so it works
12345; // this is similar to the above and will compile

I assume that the callback is being passed parameters using a macro as mentioned earlier or in some other way in the connect function.

Upvotes: 1

Patashu
Patashu

Reputation: 21793

I suspect G_CALLBACK is a macro. (Macros are expanded by the preprocessor before compilation, so what looks like invalid C may be using lots of macros.) You should look at what this macro expands to. It will help you figure out what the code is doing.

By the way, if you pass a function pointer (by taking the address of a function. e.g. &hello) to another function and you want to call it in that function, you do it something like this (*function)(arg,arg2)

Upvotes: 0

Related Questions