niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » C
Function Pointer
Post new topic   Reply to topic Page 1 of 1 [4 Posts] View previous topic :: View next topic
Author Message
Sikandar
*nix forums beginner


Joined: 21 Jul 2006
Posts: 3

PostPosted: Fri Jul 21, 2006 1:23 pm    Post subject: Function Pointer Reply with quote

typedef struct
{

int (*close)( void );
void (*enable)( bool );
void (*tx)( void );
int (*sergetchar)( bool );

}SERINTERFACE, *PSERINTERFACE;



typedef enum
{
SER_NONE,
SER_TELEGRAPH, /* telegraph plug in board */
SER_MEMORY, /* Trace messages dumped to a RAM buffer */
SER_DBGCOMM, /* the muli-ice debug comms channel */
SER_HOSTMODEM /* the host modem llserial interface */
} LLSERID;


typedef struct
{
void (*flowHandler)( bool );
void (*flowReadyHandler)( bool );
void (*cdHandler)( bool );
void (*ringHandler)( void );
void (*rxHandler)( unsigned int, char* );

} LLSERCALLBACKS, *PLLSERCALLBACKS;


static PSERINTERFACE (*llseropen[])( PLLSERCALLBACKS ) =
{ NULL,
ll_Int0,
ll_Int1,
ll_Int2,

}



PSERINTERFACE llser_open( LLSERID id, PLLSERCALLBACKS pcb )
{

if( (id < 1) || (id > (sizeof( llseropen )/sizeof( PLLSERINTERFACE
)) ) )
{
return( NULL );
}
else
{
if (llseropen[id] != NULL) //LINE 1
{
return( llseropen[id]( pcb ) ); // LINE 2
}
else
{
return( NULL );

} /* endif */
}

What exactly is happening in Line 1 and line 2.


Regards in anticipation of your replies,

Sikandar
Back to top
Tak-Shing Chan
*nix forums beginner


Joined: 10 Jul 2006
Posts: 19

PostPosted: Fri Jul 21, 2006 1:37 pm    Post subject: Re: Function Pointer Reply with quote

On Fri, 21 Jul 2006, Sikandar wrote:

Quote:
[snip]

static PSERINTERFACE (*llseropen[])( PLLSERCALLBACKS ) =
{ NULL,
ll_Int0,
ll_Int1,
ll_Int2,

}



PSERINTERFACE llser_open( LLSERID id, PLLSERCALLBACKS pcb )
{

if( (id < 1) || (id > (sizeof( llseropen )/sizeof( PLLSERINTERFACE
)) ) )
{
return( NULL );
}
else
{
if (llseropen[id] != NULL) //LINE 1
{
return( llseropen[id]( pcb ) ); // LINE 2
}

[snip]

What exactly is happening in Line 1 and line 2.

Line 1: makes sure that llsropen[id] is not NULL.

Line 2: call the function pointed to by llsropen[id] with
argument pcb. This is equivalent to:

switch (id) {
case 1:
ll_Int0(pcb);
break;
case 2:
ll_Int1(pcb);
break;
case 3:
ll_Int2(pcb);
}

Tak-Shing
Back to top
Chris Dollin
*nix forums Guru


Joined: 24 Feb 2005
Posts: 373

PostPosted: Fri Jul 21, 2006 1:41 pm    Post subject: Re: Function Pointer Reply with quote

Sikandar wrote:

(fx:snip)

Quote:
static PSERINTERFACE (*llseropen[])( PLLSERCALLBACKS ) =
{ NULL,
ll_Int0,
ll_Int1,
ll_Int2,

}

PSERINTERFACE llser_open( LLSERID id, PLLSERCALLBACKS pcb )
{

if( (id < 1) || (id > (sizeof( llseropen )/sizeof( PLLSERINTERFACE
)) ) )
{
return( NULL );
}
else
{
if (llseropen[id] != NULL) //LINE 1
{
return( llseropen[id]( pcb ) ); // LINE 2
}
else
{
return( NULL );

} /* endif */
}

What exactly is happening in Line 1 and line 2.

In line 1, a function pointer is being tested to see if it's
not null.

[The function pointer is the id'th element of the `llseropen`
array.]

In line 2, a that now-known-to-be-non-null function pointer is
being called.

The coder appears to be over-fond of CAPITALS, impenetrable names,
and excessive use of punctuation, so they're probably stuck with
some legacy coding standard.

--
Chris "opinion offered in passing" Dollin
"People are part of the design. It's dangerous to forget that." /Star Cops/
Back to top
Ancient_Hacker
*nix forums beginner


Joined: 31 May 2006
Posts: 42

PostPosted: Fri Jul 21, 2006 1:41 pm    Post subject: Re: Function Pointer Reply with quote

Sikandar wrote:

This struct holds four pointers to functions, some taking no
parameter, some taking a bool.


Quote:
typedef struct
{

int (*close)( void );
void (*enable)( bool );
void (*tx)( void );
int (*sergetchar)( bool );

}SERINTERFACE, *PSERINTERFACE;


This typedef defines some serial id identifiers.

Quote:
typedef enum
{
SER_NONE,
SER_TELEGRAPH, /* telegraph plug in board */
SER_MEMORY, /* Trace messages dumped to a RAM buffer */
SER_DBGCOMM, /* the muli-ice debug comms channel */
SER_HOSTMODEM /* the host modem llserial interface */
} LLSERID;




Another struct of function pointers.

Quote:
typedef struct
{
void (*flowHandler)( bool );
void (*flowReadyHandler)( bool );
void (*cdHandler)( bool );
void (*ringHandler)( void );
void (*rxHandler)( unsigned int, char* );

} LLSERCALLBACKS, *PLLSERCALLBACKS;


This actually declares and sets up an array of functions, indexed by
the above enums..


Quote:
static PSERINTERFACE (*llseropen[])( PLLSERCALLBACKS ) =
{ NULL,
ll_Int0,
ll_Int1,
ll_Int2,

}


This apparently is a function you call with a serial id enum and a pcb
index,

first it checks to ensure that id is >= 0 and <= the highest index in
llseropen.
otherwise it returns NULL,

then if the index is okay, (IN LINE1): it checks the indexth entry in
the llseropen table, if its null it immediately returns NULL,

if the pointer isnt null, (IN LINE 2): it calls a function in the
table,, passing it the pcb value, then returrns that function's return
value.

This is somewhat tricky code, as the function pointers have to be set
just right.



Quote:
PSERINTERFACE llser_open( LLSERID id, PLLSERCALLBACKS pcb )
{

if( (id < 1) || (id > (sizeof( llseropen )/sizeof( PLLSERINTERFACE
)) ) )
{
return( NULL );
}
else
{
if (llseropen[id] != NULL) //LINE 1
{
return( llseropen[id]( pcb ) ); // LINE 2
}
else
{
return( NULL );

} /* endif */
}

What exactly is happening in Line 1 and line 2.


Regards in anticipation of your replies,

Sikandar
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [4 Posts] View previous topic :: View next topic
The time now is Sat Nov 22, 2008 9:02 pm | All times are GMT
navigation Forum index » Programming » C
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Arbitrary function with parameter darknails@gmail.com C++ 2 Fri Jul 21, 2006 9:58 am
No new posts Is there C/C++ corresponding function in Linux for Java's... xiebopublic@gmail.com apps 4 Fri Jul 21, 2006 3:22 am
No new posts Is there C/C++ corresponding function in Linux for Java's... xiebopublic@gmail.com C++ 1 Fri Jul 21, 2006 2:44 am
No new posts can I call a internal function directly? minrobin@gmail.com shell 2 Fri Jul 21, 2006 2:17 am
No new posts determine pointer to point to array or single item during... yancheng.cheok@gmail.com C++ 5 Fri Jul 21, 2006 1:17 am

Debt Consolidation | Free Wordpress Blogs Hosting | Personal Loans | Camilo jose cela | Mobile Phones
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1757s ][ Queries: 16 (0.0732s) ][ GZIP on - Debug on ]