473,385 Members | 1,341 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,385 software developers and data experts.

Function Pointer

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

Jul 21 '06 #1
3 2075
On Fri, 21 Jul 2006, Sikandar wrote:
[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
Jul 21 '06 #2

Sikandar wrote:

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

typedef struct
{

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

}SERINTERFACE, *PSERINTERFACE;
This typedef defines some serial id identifiers.
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.
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..

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.
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
Jul 21 '06 #3
Sikandar wrote:

(fx: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
}
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/

Jul 21 '06 #4

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

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
2
by: sushil | last post by:
+1 #include<stdio.h> +2 #include <stdlib.h> +3 typedef struct +4 { +5 unsigned int PID; +6 unsigned int CID; +7 } T_ID; +8 +9 typedef unsigned int (*T_HANDLER)(void); +10
27
by: Marlene Stebbins | last post by:
I am experimenting with function pointers. Unfortunately, my C book has nothing on function pointers as function parameters. I want to pass a pointer to ff() to f() with the result that f() prints...
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: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
54
by: John | last post by:
Is the following program print the address of the function? void hello() { printf("hello\n"); } void main() { printf("hello function=%d\n", hello); }
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
20
by: MikeC | last post by:
Folks, I've been playing with C programs for 25 years (not professionally - self-taught), and although I've used function pointers before, I've never got my head around them enough to be able to...
10
by: Richard Heathfield | last post by:
Stephen Sprunk said: <snip> Almost. A function name *is* a pointer-to-function. You can do two things with it - copy it (assign its value to an object of function pointer type, with a...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.