A
Angus Comber
Hello
Here is my code so far. Is this correct/incorrect/along the right
lines/other?
#include <stdio.h>
#include <string.h>
#include <search.h>
struct mystruct
{
long nKey;
char szIP[20];
};
int compare( const void *long1, const void *long2 );
int main()
{
mystruct devlist[4];
devlist[0].nKey = 9;
strcpy(devlist[0].szIP, "192.168.1.1");
devlist[1].nKey = 2;
strcpy(devlist[1].szIP, "192.168.1.2");
devlist[2].nKey = 7;
strcpy(devlist[2].szIP, "192.168.1.3");
devlist[3].nKey = 1;
strcpy(devlist[3].szIP, "192.168.1.4");
qsort( (void *)devlist, (size_t)4, sizeof( char * ), compare );
// bsearch is the next thing to work out how to do!
// void *bsearch( const void *key, const void *base, size_t num, size_t
width, int
// ( __cdecl *compare ) ( const void *elem1, const void *elem2 ) );
return 0;
}
int compare( const void* val1, const void* val2 )
{
struct mystruct *sp1 = (mystruct*)val1;
struct mystruct *sp2 = (mystruct*)val2;
// IS THIS CORRECT?
return (int)(sp1->nKey - sp2->nKey);
}
Angus Comber
(e-mail address removed)
Here is my code so far. Is this correct/incorrect/along the right
lines/other?
#include <stdio.h>
#include <string.h>
#include <search.h>
struct mystruct
{
long nKey;
char szIP[20];
};
int compare( const void *long1, const void *long2 );
int main()
{
mystruct devlist[4];
devlist[0].nKey = 9;
strcpy(devlist[0].szIP, "192.168.1.1");
devlist[1].nKey = 2;
strcpy(devlist[1].szIP, "192.168.1.2");
devlist[2].nKey = 7;
strcpy(devlist[2].szIP, "192.168.1.3");
devlist[3].nKey = 1;
strcpy(devlist[3].szIP, "192.168.1.4");
qsort( (void *)devlist, (size_t)4, sizeof( char * ), compare );
// bsearch is the next thing to work out how to do!
// void *bsearch( const void *key, const void *base, size_t num, size_t
width, int
// ( __cdecl *compare ) ( const void *elem1, const void *elem2 ) );
return 0;
}
int compare( const void* val1, const void* val2 )
{
struct mystruct *sp1 = (mystruct*)val1;
struct mystruct *sp2 = (mystruct*)val2;
// IS THIS CORRECT?
return (int)(sp1->nKey - sp2->nKey);
}
Angus Comber
(e-mail address removed)