You are not logged in.

#1 2010-04-09 17:18:05

neok
Member
From: Cyprus
Registered: 2003-12-14
Posts: 190
Website

How to pass a pointer to a struct to GTK callback function

Hi,

This is getting as frustrating as it is embarrasing - I want to use the "gpointer user_data" argument of a GTK+ callback function to set the value of two variables in another part of the program. In this simplified example I want to use user_data to set the value of two int's in a structure:

typedef struct { int x, y; } xy_t;

void some_callback( GtkWidget *widget, gpointer user_data )
{
    (xy_t *) user_data->x = some_value;
    (xy_t *) user_data->y = other_value;
    ............
  }

However I get the following error message: ...dereferencing 'void *' pointer

Not withstanding <my stupid mistakes>, what is the way to pass on more than one user variable to a GTK+ callback function?

My thanks in advance.


Regards

Neoklis ... Ham Radio Call: 5B4AZ

Offline

#2 2010-04-09 17:25:13

FOODy
Member
From: Germany
Registered: 2009-10-07
Posts: 5

Re: How to pass a pointer to a struct to GTK callback function

Have you tried the following:

typedef struct { int x, y; } xy_t;

void some_callback( GtkWidget *widget, gpointer user_data )
{
    ((xy_t *) user_data)->x = some_value;
    ((xy_t *) user_data)->y = other_value;
    ............
}

It may be that you cast the member (x / y) of user_data instead of user_data itself.

Offline

#3 2010-04-09 17:37:01

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 859
Website

Re: How to pass a pointer to a struct to GTK callback function

Or to avoid that ugly cast every time:

typedef struct { int x, y; } xy_t;

void some_callback( GtkWidget *widget, gpointer user_data )
{
    xy_t *point = user_data;
    point->x = some_value;
    point->y = other_value;
    ............
}

Offline

#4 2010-04-09 17:40:24

neok
Member
From: Cyprus
Registered: 2003-12-14
Posts: 190
Website

Re: How to pass a pointer to a struct to GTK callback function

FOODy wrote:

Have you tried the following:

typedef struct { int x, y; } xy_t;

void some_callback( GtkWidget *widget, gpointer user_data )
{
    ((xy_t *) user_data)->x = some_value;
    ((xy_t *) user_data)->y = other_value;
    ............
}

It may be that you cast the member (x / y) of user_data instead of user_data itself.

Ahh, thanks! It worked - well, except that I now got "error: 'some_value' undeclared" ;-)

Now I can go to bed less embarrassed - I would never have thought of that!


Regards

Neoklis ... Ham Radio Call: 5B4AZ

Offline

#5 2010-04-09 17:47:18

neok
Member
From: Cyprus
Registered: 2003-12-14
Posts: 190
Website

Re: How to pass a pointer to a struct to GTK callback function

tavianator wrote:

Or to avoid that ugly cast every time:

typedef struct { int x, y; } xy_t;

void some_callback( GtkWidget *widget, gpointer user_data )
{
    xy_t *point = user_data;
    point->x = some_value;
    point->y = other_value;
    ............
}

Exactly this is what was driving up the wall - I tried this and it was accepted, so I just couldn't understand why the ugly casting form wasn't. But I also assumed that this form would not give me the right results, so I didn't even try it in practice. And all this after writing a fair number of apps for Linux... ;-)


Regards

Neoklis ... Ham Radio Call: 5B4AZ

Offline

Board footer

Powered by FluxBB