Unix/Linux Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Help me to understand strange 'typedef ... ' in some source...

Programming


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 10-13-2016
alex_5161 alex_5161 is offline
Registered User
 
Join Date: Jan 2007
Last Activity: 13 October 2016, 4:35 PM EDT
Location: Detroit
Posts: 189
Thanks: 21
Thanked 2 Times in 2 Posts
Help me to understand strange 'typedef ... ' in some source...

Working on some source I've found some strange declaration in included header file.
I am looking for someone's help to understand me that syntax's, as it is fine (it is compiled without any complain,) but for me it seems out of any sense!
Acctually, it warning by CC compiler: " Warning: Implicit int is not supported in C++". That why I have checked that.

Code:
#ifdef WIN32 
typedef (_cdecl *COMPARE_ADDR)(void *,void*); 
#else 
extern "C" { 
typedef (*COMPARE_ADDR)(void *,void*); 
} 
#endif

I see it is preprared for diffrerent OS - Windows or Unix, but neither one 'typedef ..' I could understand

The 'typedef' should have [original_type] that will be reffered by the [user_defined_type].
So, it should be
typedef <initial_type> <new_name_for_initial_type>;

But here it seems as a casting or some function referencing syntax...
I could not understand what that means!

Could anyone give me a hint what that could be?

Thanks!

Last edited by alex_5161; 10-13-2016 at 04:34 PM..
Sponsored Links
    #2  
Old Unix and Linux 10-13-2016
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 29 March 2017, 12:49 AM EDT
Location: NM
Posts: 10,992
Thanks: 506
Thanked 1,034 Times in 960 Posts
That is a typedef of a function. In this case the need was for portability -windows compiler versus maybe a linux C compiler.
No dataype declaration for the function. So, by default, the function will compile probably with a warning as a function that returns an integer.

So you do not worry about the platform, you will notice void * arguments. This lets you call COMPARE_ADDR with any datatypes as long as those datatypes are by reference (address of the object). It does not have a datatype for the function return. This kind of function typedef is usually done for situations where you want to call something to get a standard result, but windows and linux (example platform) do not have the same name for the function, but it may return { <0, 0, >0} for less than, equal to, or greater than.

Pretend example

Code:
#ifdef WIN32 
   COMPARE_ADDR=isequal;
#else
   COMPARE_ADDR=addr_foo;
#endif
/* see if the memory location of two variables is the same */
if  ( ! COMPARE_ADDR( &variable3, &variable1) {  /* if equal barf */
    errno=EINVAL;  /* invalid value error code */
    perror("Cannot continue, variables are identical and must not be identical\n");
    exit(1);   /* end program here */
}


Last edited by jim mcnamara; 10-13-2016 at 10:00 PM..
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Typedef does not work to name a type yifangt Programming 11 02-19-2015 01:25 PM
typedef help giampoul Programming 3 03-31-2012 06:45 PM
Need help with this c++ source code! DOnt understand what some stuff mean. orszhak Programming 2 04-09-2011 01:00 PM
bash shell: 'exec', 'eval', 'source' - looking for help to understand alex_5161 Shell Programming and Scripting 3 07-24-2008 12:42 PM
How to typedef krishna_sicsr Programming 1 05-31-2007 11:04 PM



All times are GMT -4. The time now is 09:10 AM.