• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 230
  • Last Modified:

How do you point a struct pointer to a function ??

I am wanting to know how this is done.What made me start to think about this is cause I was messing with Gtk on linux and I noticed this GtkWidget *window,I take it GtkWidget is a typedef struct pointer but this pointer is pointing to a function,I have tried to do the same with a simple program but I have failed.So the ? is how do you point a struct pointers to a function

 GtkWidget *window;
  gtk_init(&argc, &argv);
   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   

Thank you for your reply and time.
0
DWA
Asked:
DWA
1 Solution
 
codez80Commented:
could you tell us a little bit more...

codez80
0
 
DWAAuthor Commented:
What do you need to know???


 GtkWidget *window;//struct pointer

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);//pointer pointing to a function

how can I point a struct pointer to a function as they did in this code.I just want the general concept.

Thank you for your time and help
0
 
ExceterCommented:
>> is how do you point a struct pointers to a function

I'm not really sure what you mean by this. Could you explain in greater detail?

Exceter
0
Free tool for managing users' photos in Office 365

Easily upload multiple users’ photos to Office 365. Manage them with an intuitive GUI and use handy built-in cropping and resizing options. Link photos with users based on Azure AD attributes. Free tool!

 
gj62Commented:
So far as I know,  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);//pointer pointing to a function

is NOT a pointer pointing to a function.  It returns a pointer to a new instance of GtkWidget...
0
 
ExceterCommented:
>> window = gtk_window_new(...

This is not setting window to point at this function. gtk_window_new is simply returning a pointer to an instance of GtkWidget. It is done in exactly same manner that any other pointer is returned. The only difference is what the pointer points to. For example,

typedef struct st
{
     int x;
}st;

st* test( st* );

int main()
{
     st *s;
     s = test( s );
     printf("%i\n",s->x);

     return 0;
}

st* test( st* i )
{
     i = (st*)malloc( sizeof(st) );
     i->x = 6;
     return i;
}

Exceter
0
 
ExceterCommented:
Oops, I forget to free s.

free(s);

Exceter
0
 
TheBeaverCommented:
"...but this pointer is pointing to a function..."

Actually it is not pointing to a function it is merely being loaded with whatever that function returns.

You CAN have function pointers like this...

char function(char blah)
{
  // Do stuff
  return 1;
}

main()
{
  char (*flogalog)() = (void *)function;
  char result = flogalog(2);
}

..but I dont think you mean that judging by your question.
0
 
DWAAuthor Commented:
Thanks to you all.
0
 
ExceterCommented:
You are most welcome.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now