Jump to content

Function prototypes and definitions?

- - - - -

  • Please log in to reply
2 replies to this topic

#1
alirezan

alirezan

    Learning Programmer

  • Members
  • PipPipPip
  • 62 posts
Hi
I'm going through this code to try to understand it but everything about it is strange. I don't even understand it. Can someone help me understand it please?

Here are chunks of the code:

in the .h file:
//  [serialOpen]  Arg# -- Description -----------------------

//                1       (const vt_char *) pathname / com dev. description

//                2       (vt_int32)        flags

//                return  (vt_int32)        descriptor handle

//  [serialWrite] Arg# -- Description -----------------------

//                1       (vt_int32)        descriptor handle

//                2       (const vt_char *) buffer

//                3       (vt_uint32)       buffer data len

//  [serialRead]  Arg# -- Description -----------------------

//                1       (vt_int32)        descriptor handle

//                2       (vt_char *)       buffer

//                3       (vt_uint32)       buffer space len

//  [serialClose] Arg# -- Description -----------------------

//                1       (vt_int32)        descriptor handle

//                return  (vt_int32)        0 on success, -1 on failure

//  [serialErrno] Arg# -- Description -----------------------

//                return  (vt_int32)        errno, see errno.h (GNU C Compiler Version) (*)

//

// (*) The error numbers of interest are; 

//      #define	EIO 5		        /* I/O error (used as a generic error) */

//      #define	EAGAIN 11	        /* No more processes */

//      #define EWOULDBLOCK EAGAIN	/* Operation would block */

//      EIO is treated as a generic error condition.

//

//  functions behave the same as the standard stdio & errno calls

//  (open, close, read, write, errno)

//

//  any function that returns -1 should update a local copy of 

//  their errno appropriately. 

// ------------------------------------------------------*/

typedef vt_int32 (*serialOpen)  ( const vt_char *, vt_int32 );

typedef vt_int32 (*serialWrite) ( vt_int32, const vt_char *, vt_uint32 );

typedef vt_int32 (*serialRead)  ( vt_int32, vt_char *, vt_uint32 );

typedef vt_int32 (*serialClose) ( vt_int32 );

typedef vt_int32 (*serialErrno) ( void );


//  Change the serial API to the physical serial port. 

//  Call this function after opening the channel, but before 

//  using it.

// Arguements -----------------------------------------------

//  (serialOpen)        function to open serial port

//  (serialWrite)       function to write to port (out to far end device)

//  (serialRead)        function to read from port (in from far end device)

//  (serialClose)       function to close serial port

//  (serialErrno)       function to return the last error

// Returns --------------------------------------------------

//  (vt_int32)          VCL_OK, operation succeeded

//                      VCL_ERROR, operation failed

// Notes ----------------------------------------------------

// --------------------------------------------------------*/

vt_int32 vcl_ChangeSerInterface( serialOpen, serialWrite, 

    serialRead, serialClose, serialErrno );


First of all, right there...Why would someone do that? Why would you input the functions to another function?

#if (USE_SERLIB==1)

serialOpen       serOpen  = slOpen;

serialWrite      serWrite = slWrite;

serialRead       serRead  = slRead;

serialClose      serClose = slClose;

serialErrno      serErrNo = slErrno;

static vt_flag   serIsInit = TRUE;

#else

serialOpen       serOpen  = NULL;

serialWrite      serWrite = NULL;

serialRead       serRead  = NULL;

serialClose      serClose = NULL;

serialErrno      serErrNo = NULL;

static vt_flag   serIsInit = FALSE;

#endif


Here is the definition of vcl_changeserinterface:

vt_bool vcl_ChangeSerInterface( serialOpen sopen, serialWrite swrt, 

    serialRead sread, serialClose sclose, serialErrno serrno )

{

    if (!serIsInit)

    {

        serOpen   = sopen;

        serWrite  = swrt;

        serRead   = sread;

        serClose  = sclose;

        serErrNo  = serrno;

        serIsInit = TRUE;

        return TRUE;

    }

    else

    {

        return FALSE;

    }

}



but when you look at the definition of say "slOpen", it uses the function "open"

vt_int32 slOpen( const vt_char * chanName , vt_int32 flags )

{

    return open( (const char*)chanName, (int)flags );

}

I absolutely don't follow this. Can someone please help me figure this out?

Thanks

Edited by dargueta, 30 April 2011 - 10:58 PM.
Please use [code][/code] tags next time.


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
You're thinking way too hard. This type of thing is very common in low-level code. People regularly offer a public interface, and then immediately call a private function to do the work. They can then change the private function without changing the public interface. Passing function pointers around is also fairly common in this type of code.

What you need to focus on is: which functions are you supposed to be calling, and how?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
In addition a good motivation to use function pointers:

Suppose you have 10 different functions taking same parameters but performing individual computation.
The only standard way to call them each is like

fun1();
fun2();
.......

Won't it be nice to be able to say use a loop to do that? Or have a run time scenario which generates a result and automatically calls the function needed.

Function name is a hard coded thing that can't be changed. Function pointer is a variable in which you can assign address of any function and call it. This assignment can be done through run time calculation which is a major motivation for func ptrs.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users