473,326 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Problem with function pointer in structure

Given the following code :
#include <stdio.h>

typedef struct Handler
{
int data1 ;
int data2 ;
int data3 ;

void ( *cb ) ( Handler * ) ;
} Handler ;

void callBack ( Handler * handler ) ;

int main ( void )
{

Handler myHandler ;

myHandler.cb = callBack ;

myHandler.cb ( &myHandler ) ;
return 0 ;
}

void callBack ( Handler * handler )
{
printf ( "\nData 1 = %d\n\n", handler->data1 ) ;
}

When I compile it I get:
/export/SAN1/simonj/test/typedef gcc -std=c89 -Wall -pedantic test.c
test.c:10: error: parse error before '*' token

Any ideas ?

Thanks !
Joe

Apr 12 '07 #1
4 5826
jb******@lmco.com wrote:
Given the following code :
#include <stdio.h>

typedef struct Handler
{
int data1 ;
int data2 ;
int data3 ;

void ( *cb ) ( Handler * ) ;
} Handler ;
typedef struct Handler Handler;

struct Handler
{
int data1 ;
int data2 ;
int data3 ;

void ( *cb ) ( Handler * ) ;
};

or

typedef struct Handler
{
int data1 ;
int data2 ;
int data3 ;

void ( *cb ) ( struct Handler * ) ;
} Handler;

The typedef has to come before the use.

--
Ian Collins.
Apr 12 '07 #2
Ian Collins wrote:
jb******@lmco.com wrote:
>Given the following code :
#include <stdio.h>

typedef struct Handler
{
int data1 ;
int data2 ;
int data3 ;

void ( *cb ) ( Handler * ) ;
} Handler ;
typedef struct Handler Handler;

struct Handler
{
int data1 ;
int data2 ;
int data3 ;

void ( *cb ) ( Handler * ) ;
};

or

typedef struct Handler
{
int data1 ;
int data2 ;
int data3 ;

void ( *cb ) ( struct Handler * ) ;
} Handler;

The typedef has to come before the use.
OK thanks...
Joe
Apr 13 '07 #3
On Fri, 13 Apr 2007 00:24:20 -0400, dada wrote:
Ian Collins wrote:
>jb******@lmco.com wrote:
<snup>
>typedef struct Handler Handler;

struct Handler
{
int data1 ;
int data2 ;
int data3 ;

void ( *cb ) ( Handler * ) ;
};

or

typedef struct Handler
{
int data1 ;
int data2 ;
int data3 ;

void ( *cb ) ( struct Handler * ) ;
} Handler;

The typedef has to come before the use.

OK thanks...
Joe
There is an irritating wee gotcha if you want to define
a type for the call back. For that you need something like

struct bob;
typedef void (*bob_cb)( struct bob* );
typedef struct bob
{ int d;
bob_cb f;
} Bob;
(That is, you need a forward declaration of struct bob).

By analogy with
typedef struct joe
{ int d;
struct jim* f;
} Joe;
struct jim
{ int d;
};
which is ok, I had in my ignorance thought

typedef void (*cath_cb)( struct cath* );
struct cath
{ int d;
cath_cb f;
};
would be ok, but in fact it isn't (as the kind folks
here explained)
Duncan
Apr 13 '07 #4
In article <pa****************************@csl.co.uk>
Duncan Muirhead <dm***@csl.co.ukwrote:
>There is an irritating wee gotcha if you want to define
a type for the call back. ...
Not if you follow my recommendations.
See <http://web.torek.net/torek/c/types2.html>.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Apr 13 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
4
by: quek | last post by:
Im having a problem passing a struct ptr. Maybe someone could help me. Heres what Visual C gives me as error: error C2664: 'adpcm_coder_u' : cannot convert parameter 4 from 'struct main::$S1 *'...
26
by: Adam Warner | last post by:
Hello all, I'm very new to C but I have a number of years of Common Lisp programming experience. I'm trying to figure out ways of translating higher order concepts such as closures into C. The...
17
by: Razzel | last post by:
I created this as a test: #include <time.h> main(){ printf(X1: %s\n", putim()); printf(X2: %s\n", putim()); } putim() { time_t t; time(&t); return(ctime(&t));
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
1
by: Nikolay Petrov | last post by:
I use some of Windows Terminal Services APIs in my app. The is an API function which enumerates sessions on a terminal server. It uses a pointer from another API function to know on which...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
23
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when...
3
by: is_vlb50 | last post by:
I have problem with pass structure to thread function in SOLARIS.When I pass it to created thread it always has empty values. my structure defined as: typedef struct momMSG{ struct mbhdr hd;...
19
by: ballpointpenthief | last post by:
At the moment, I only write functions which accept pointers as arguments in order to change a value, but I've realised it would often be a lot more effiecient to pass a pointer rather than an...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.