0

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 Answers 2

1
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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the reply andlabs. Yes I saw that too late and was editing my post while you replied. I also edited the code and compiled. The post has been updated to the code as it is now, I think your right I also had a mistake in the function right(). Ill digest what your saying and try to fix things up. When I get it going Ill give the tick.
Thanks For the reply andlabs. What I dont ubderstand is char on = "y", if it didnt exist before I initialised it, how did it turn from a pointer to an int. I hoped I was declaring and initialising a variable not a pointer. You can probably see what I am trying to do. Am I going about it in the wrong way? How to syntax the gpointer data paramater of the call back line I could use some help. There was a mistake in right(), used the wrong one, buf set to 250, so that explains that. I would be happy to just get the gtk side of things right.
Tried to explain the warnings; hope that helps too!
Yes very helpful, I took from that the pointer "y" is somewhere in the gtk headers or even deeper, because its not in any of the code that Im compiling. One last question. Some other buttons in this window did a good job connecting and disconnecting a bluetooth socket to an arduino. Can gtk pass actual constants and variables like integers from slider values or constants from buttons or does it deal only with pointers.
Signals can only carry pointers. You can pass pointers to the widgets if you like. Alternatively, if you have GObjects controlling your Arduino, you can bind GObject properties together; this is much more advanced though, and too involved for a comment. Also "y" isn't in the GTK+ headers; it's created by the compiler. const char *mystring = "y"; sets mystring to some memory address such that mystring[0] == 'y' and mystring[1] == '\0'. That memory address is decided by the compiler; it'll be part of the compiled binary image and copied into RAM each execute. This is how C strings work.
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.