?
Solved

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

Posted on 2003-02-22
9
Medium Priority
?
219 Views
Last Modified: 2010-04-15
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
Comment
Question by:DWA
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 1

Expert Comment

by:codez80
ID: 7999929
could you tell us a little bit more...

codez80
0
 

Author Comment

by:DWA
ID: 7999966
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
 
LVL 8

Expert Comment

by:Exceter
ID: 7999973
>> 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:gj62
ID: 8000002
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
 
LVL 8

Accepted Solution

by:
Exceter earned 80 total points
ID: 8000013
>> 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
 
LVL 8

Expert Comment

by:Exceter
ID: 8000016
Oops, I forget to free s.

free(s);

Exceter
0
 
LVL 1

Expert Comment

by:TheBeaver
ID: 8000080
"...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
 

Author Comment

by:DWA
ID: 8000116
Thanks to you all.
0
 
LVL 8

Expert Comment

by:Exceter
ID: 8000412
You are most welcome.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question