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


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 10-13-2016   -   Original Discussion by alex_5161
alex_5161 alex_5161 is offline
Registered User
 
Join Date: Jan 2007
Last Activity: 1 November 2017, 1:04 PM EDT
Location: Detroit
Posts: 195
Thanks: 24
Thanked 3 Times in 3 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 05:34 PM..
Sponsored Links
    #2  
Old Unix and Linux 10-13-2016   -   Original Discussion by alex_5161
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 22 November 2017, 2:26 PM EST
Location: NM
Posts: 11,240
Thanks: 571
Thanked 1,117 Times in 1,031 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 11:00 PM..
Sponsored Links
Closed

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 02:25 PM
typedef help giampoul Programming 3 03-31-2012 07:45 PM
Need help with this c++ source code! DOnt understand what some stuff mean. orszhak Programming 2 04-09-2011 02:00 PM
bash shell: 'exec', 'eval', 'source' - looking for help to understand alex_5161 Shell Programming and Scripting 3 07-24-2008 01:42 PM
How to typedef krishna_sicsr Programming 1 06-01-2007 12:04 AM



All times are GMT -4. The time now is 03:05 AM.