Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

qsort and bsearch warning at compilation... 1

Status
Not open for further replies.

fl0ra

MIS
Jul 27, 2004
93
FR
Code:
#define L_VARIABLE 0xFF
#define N_VARIABLE 173

char sDS_PKG_VariableName[N_VARIABLE][L_VARIABLE]={"ANT_PC_GPS","AS2DEG","BOLTZ","CHANNEL_ID","CLK_ERR_GPS_SAT","CLOCK_BIAS","COARSE_OBT0","COARSE_UTC0","COR_SPAC","CPM_CORR_L1","CPM_CORR_L2","CPM_L1","CPM_L2","C_A_PRM_CORR_SM","C_A_PRM_L1","C_L","C_PN","ClockOffset","ClockTimetag","DATA_THRES",/* 173 variables name I'm obviously not pasting them all... */
}

qsort(sDS_PKG_VariableName,N_VARIABLE,L_VARIABLE,strcmp);
if(bsearch("C_L",sDS_PKG_VariableName,N_VARIABLE,L_VARIABLE,strcmp)==NULL) {
/* blabla */
} else {
/* blabla */
}

ok nothing really special about it.
However when I compile with gcc it gives me those warnings:

warning: passing arg 4 of 'qsort' for incompatible pointer type
warning: passing arg 5 of 'bsearch' for incompatible pointer type


At the end of the day it works fine.
But I am wondering why do I get warnings like that because it is really "by-the-book" example of use qsort and bsearch.

Cheers
 
Of course, qsort() signature is:
Code:
void qsort(void*, size_t, size_t, int (*)(const void*, const void*));
but strcmp() signature is:
Code:
int strcmp(const char*, const char*);
No any standard conversions between pointers to functions...
Well, in that case you may use brutal force (and controlled lie) method:
Code:
typedef int (*QScmpf)(const void*,const void*);
...
qsort(...,...,...,(QScmpf)strcmp);
It will be OK (in this case)...
 
Some additions:
Strictly speaking (to conform with C Standard), in that cases we must write a minimal wrapper:
Code:
#ifdef __cplusplus
inline /* optional */
#endif
int StrCmp(const void* s1, const void* s2)
{
  return strcmp((const char*)s1,(const char*)s2);
}
...
qsort(....,StrCmp);
The (const char*) cast is optional in C, but we must do it in C++.
Apropos, use the same approach(es) for bsearch()...
 
yep. the first approach works great (obviously).
I'm gonna stick with that.

thank you very much indeed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top