tags:

views:

83

answers:

3

See I'm using glib, and gpointer is a typedef of void *. (glib did this type to make things clear, I guess).

when I make a signal connect, I have to pass the data as void pointer (void *).

so the code is (something like this) :

...
g_signal_connect (object, function, (gpointer) data);
...

If I use short as data type, I get a warning message of gcc like this:

warning: cast to pointer from integer of different size

If I use int as data type, I get no warnings.

But in both cases, everything works well, so, why I get this warning using short?

+2  A: 

On a 32 bit architecture, a pointer is 32 bits. An int is usually 32 bits as well - so the conversion is '1 to 1'. A short is usually 2 bytes, so casting a short to a pointer isn't normally a very safe things to do - which is why the compiler warns you.

sje397
+1  A: 

A short is 2 Bytes a pointer is platform dependant but usually 4 or 8 Bytes. That should be why you get that error. You probably want to pass a the reference of the short in which would be:

(gpointer) &data);
DrDipshit
Passing a reference, I don't get the correct value
drigoSkalWalker
Well you would need to dereference it in your callback. IE: short s = *data;
DrDipshit
bstpierre
Good point. I had not thought of that. Its been a little while since I last worked with Gtk and C.
DrDipshit
In this case, that can be solved by making the `short` variable `static`.
caf
+2  A: 

Gcc is telling you that you cast an integer of different size to a pointer, which is dangerous mainly the other way you have it (eg. from a larger datatype to a smaller one). To silence the warning, you can use intptr_t as an intermediate type.

g_signal_connect (object, function, (gpointer)(intptr_t) data);
jpalecek