0
votes

How can I get the variable from gpointer so I can use it.

void
right_y (GtkButton *button, gpointer on)
{
   right(on);
}

Here is the callback part The line with char on = "y"; gives warning: initialization makes integer from pointer without a cast [-Wint-conversion]

The gsignal line gives warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]

char on = "y";
button = gtk_button_new_with_label ("Led On");                        
g_signal_connect (button, "clicked", G_CALLBACK (right_y), (gpointer) on);

Ive tried this a few different ways and still cant get it.

Thanks.

2

2 Answers

1
votes
char on = "y";

declares on as holding a single char. Strings are more than one char, so you need to either store them as pointers to char or to an array of chars.

The problem is what you want to be able to do with on determines the correct type to use. But I imagine given the nature of the errors in the post that you already know this and are confused by the gpointer.

gpointer is basically

typedef void *gpoiner;

This is why you don't need to specify a * with gpointer. Don't let this fool you; when you use other pointer types alongside gpointer, you still need the * on those other pointer types!

Also the g_signal_connect() line is wrong; gpointer on makes no sense.

Please let me know if you still don't understand what I am talking about.


Okay, so why does

char on = "y";

spit out the message

warning: initialization makes integer from pointer without a cast [-Wint-conversion]

?

"y" is a pointer. It evaluates to an expression of type const char *. The pointer will likely reside in the code section of the executable, where it cannot be modified (hence the const).

char is an integer, not a pointer. You're normally not supposed to be able to shove pointer values into integers unless you explicitly ask for it, because you might wind up using an integer type that is too small to store the pointer. (For those times when it is necessary, the C standard provides intptr_t and uintptr_t.) In this case, you didn't ask, so the compiler wants to make sure you really meant that you wanted to shove the pointer's address in a char.

Note the part about using a smaller integer than necessary. That's why your g_signal_connect() line prints the warning it does: char is not big enough to store a pointer, so treating a char value like a pointer is bound to produce really bad things.

0
votes

This is the code I was looking for.

The code concerning the callback.

char *on = 'n';
  button = gtk_button_new_with_label ("Led On");                        
  g_signal_connect (button, "clicked", G_CALLBACK (right_y),  on);

The code for the user fuction.

static void
right_y (GtkButton *button, gpointer on)
{
   int led_on = on;
   right(led_on);
}    

And a relevant snippet from function right()

void right(char state) {
   buf[0] = state;
   int  bytes_written  = 0;  
   bytes_written  = write(s, buf, strlen(buf));   
} 

The line char *on = 'n'; gives the warning

initialization makes pointer from integer without a cast [-Wint-conversion]

I think this is ok because although there are probably better ways to initialise a pointer, a pointer is what was intended.

The line int led_on = on; gives the warning

initialization makes integer from pointer without a cast [-Wint-conversion] 

Again there might be better ways, but the intention was to pass a variable not a pointer. Ive tested it a few times and it hasnt failed so Ill take it.