LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-09-2005, 10:23 PM   #1
MylesCLin
Member
 
Registered: Sep 2004
Location: Texas, USA
Distribution: Slack 9.1 with slackware-current packages...
Posts: 164

Rep: Reputation: 30
callbacks in C?[RESOLVED]


Hey, me again, still on Winblows.
I'm trying to, in C, have a pointer to a function in a structure.
So, for instance, struct object has a variable defined like so;
Code:
void *callback
and I would set this with a pointer to a function, then try and call the func like so;
Code:
items[1].callback();
The compiler complained about not being able to find the func, and then I fixed that, now it produces no errors, but the function doesn't print anything like it's supposed to.

Here's the code I have so far;

here's the item struct;
Code:
typedef struct item{
int location;
char name[20],desc[20];
void *callback;
} item;
Here's what configs the item;
Code:
void configitem(struct item *item,char name[20],char desc[50],int location,void *callback){
memcpy(item->desc, desc, strlen(desc));
memcpy(item->name, name, strlen(name));
item->location=location;
item->callback = callback;
}
and I call that like so;
Code:
configitem(&items[1],"Gun","A Gun",1,&poodle);
and here's how I'm calling the func;
Code:
items[1].callback;
Also, when I put parentheses around the callback that I'm trying to use, it says 'term does not evaluate to function'

Last edited by MylesCLin; 02-10-2005 at 10:44 AM.
 
Old 02-09-2005, 10:43 PM   #2
MylesCLin
Member
 
Registered: Sep 2004
Location: Texas, USA
Distribution: Slack 9.1 with slackware-current packages...
Posts: 164

Original Poster
Rep: Reputation: 30
Whoops, resolved it.
I needed to create a function pointer.
A good resource is:
http://gethelp.devx.com/techtips/cpp.../10min0300.asp
 
Old 02-10-2005, 10:27 AM   #3
Aga
LQ Newbie
 
Registered: Feb 2005
Posts: 3

Rep: Reputation: 0
define variable as
void (*callback)(); // pointer to function

not
void *callback; // void pointer
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
gtk problem with callbacks mvan83 Programming 3 12-13-2005 06:07 PM
Callbacks in Squid s_araj Programming 2 07-02-2005 06:06 AM
How to create a new window using callbacks.c (Glade) skie_knite007 Programming 1 04-10-2005 12:25 PM
include for callbacks.c abs Programming 5 04-07-2004 02:39 PM
Java - Interfaces, inner classes and callbacks for events gwp Programming 3 12-09-2003 04:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration