A
Angus Comber
Hello
I have received a lot of help on my little project here. Many thanks.
I have a struct with a string and a long member. I have worked out how to
qsort the struct on both members. I can do a bsearch on the long member
(nKey) but I am struggling to do a search using the string member.
The code I am running appears below. It doesn't crash or anything. It is
just that when I do the last bsearch using "192.168.1.3" I SHOULD find the
item with nKey: 7.
Any help would be MUCH appreciated.
#include <stdio.h>
#include <string.h>
#include <search.h>
struct mystruct
{
long nKey;
char szIP[20];
};
int compare( const void *val1, const void *val2 );
int IPcompare( const void* arg1, const void* arg2 );
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");
printf("Values before qsort\n");
printf("Item %u, IP: %s, Key: %u\n", 0, devlist[0].szIP, devlist[0].nKey);
printf("Item %u, IP: %s, Key: %u\n", 1, devlist[1].szIP, devlist[1].nKey);
printf("Item %u, IP: %s, Key: %u\n", 2, devlist[2].szIP, devlist[2].nKey);
printf("Item %u, IP: %s, Key: %u\n", 3, devlist[3].szIP, devlist[3].nKey);
qsort( (void *)devlist, (size_t)4, sizeof( mystruct ), compare );
// Now print out - see if anything change
printf("Values after qsort\n");
printf("Item %u, IP: %s, Key: %u\n", 0, devlist[0].szIP, devlist[0].nKey);
printf("Item %u, IP: %s, Key: %u\n", 1, devlist[1].szIP, devlist[1].nKey);
printf("Item %u, IP: %s, Key: %u\n", 2, devlist[2].szIP, devlist[2].nKey);
printf("Item %u, IP: %s, Key: %u\n", 3, devlist[3].szIP, devlist[3].nKey);
// Now lets qsort by IP Address
qsort( (void *)devlist, (size_t)4, sizeof( mystruct ), IPcompare );
printf("Values after IP Address qsort\n");
printf("Item %u, IP: %s, Key: %u\n", 0, devlist[0].szIP, devlist[0].nKey);
printf("Item %u, IP: %s, Key: %u\n", 1, devlist[1].szIP, devlist[1].nKey);
printf("Item %u, IP: %s, Key: %u\n", 2, devlist[2].szIP, devlist[2].nKey);
printf("Item %u, IP: %s, Key: %u\n", 3, devlist[3].szIP, devlist[3].nKey);
const long nKey = 2L;
// Looking for IP by Key
printf("Looking for an IP address by key (DeviceID) - Search where nKey =
2\n");
mystruct* rr = (mystruct*) bsearch(&nKey, devlist, 4, sizeof(mystruct),
compare );
if( rr )
printf( "IP address: %s found (searched using key %u\n", rr->szIP,
rr->nKey );
else
printf( "IP Address not found!\n" );
printf("Now we look for a a key (DeviceID) by IP address - search on
192.168.1.3\n");
const char* szIP = "192.168.1.3";
// key, base search data, num, width, cmp_func
mystruct* fnd = (mystruct*) bsearch(&szIP, devlist, 4, sizeof(mystruct),
IPcompare ); // This line not working!!!
if( fnd )
printf( "nKey: %u found (searched using %s\n", fnd->nKey, fnd->szIP );
else
printf( "nKey NOT found!\n" );
return 0;
}
int compare( const void* val1, const void* val2 )
{
struct mystruct* sp1 = (mystruct*)val1;
struct mystruct *sp2 = (mystruct*)val2;
return (int)(sp1->nKey - sp2->nKey);
}
int IPcompare( const void* arg1, const void* arg2 ) // char **arg1, char
**arg2 )
{
/* Compare all of both strings: */
struct mystruct *sp1 = (mystruct*)arg1;
struct mystruct *sp2 = (mystruct*)arg2;
return _strcmpi( sp1->szIP, sp2->szIP );
}
The output I get is:
Values before qsort
Item 0, IP: 192.168.1.1, Key: 9
Item 1, IP: 192.168.1.2, Key: 2
Item 2, IP: 192.168.1.3, Key: 7
Item 3, IP: 192.168.1.4, Key: 1
Values after qsort
Item 0, IP: 192.168.1.4, Key: 1
Item 1, IP: 192.168.1.2, Key: 2
Item 2, IP: 192.168.1.3, Key: 7
Item 3, IP: 192.168.1.1, Key: 9
Values after IP Address qsort
Item 0, IP: 192.168.1.1, Key: 9
Item 1, IP: 192.168.1.2, Key: 2
Item 2, IP: 192.168.1.3, Key: 7
Item 3, IP: 192.168.1.4, Key: 1
Looking for an IP address by key (DeviceID) - Search where nKey = 2
IP address: 192.168.1.2 found (searched using key 2
Now we look for a a key (DeviceID) by IP address - search on 192.168.1.3
nKey NOT found!
Press any key to continue
I have received a lot of help on my little project here. Many thanks.
I have a struct with a string and a long member. I have worked out how to
qsort the struct on both members. I can do a bsearch on the long member
(nKey) but I am struggling to do a search using the string member.
The code I am running appears below. It doesn't crash or anything. It is
just that when I do the last bsearch using "192.168.1.3" I SHOULD find the
item with nKey: 7.
Any help would be MUCH appreciated.
#include <stdio.h>
#include <string.h>
#include <search.h>
struct mystruct
{
long nKey;
char szIP[20];
};
int compare( const void *val1, const void *val2 );
int IPcompare( const void* arg1, const void* arg2 );
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");
printf("Values before qsort\n");
printf("Item %u, IP: %s, Key: %u\n", 0, devlist[0].szIP, devlist[0].nKey);
printf("Item %u, IP: %s, Key: %u\n", 1, devlist[1].szIP, devlist[1].nKey);
printf("Item %u, IP: %s, Key: %u\n", 2, devlist[2].szIP, devlist[2].nKey);
printf("Item %u, IP: %s, Key: %u\n", 3, devlist[3].szIP, devlist[3].nKey);
qsort( (void *)devlist, (size_t)4, sizeof( mystruct ), compare );
// Now print out - see if anything change
printf("Values after qsort\n");
printf("Item %u, IP: %s, Key: %u\n", 0, devlist[0].szIP, devlist[0].nKey);
printf("Item %u, IP: %s, Key: %u\n", 1, devlist[1].szIP, devlist[1].nKey);
printf("Item %u, IP: %s, Key: %u\n", 2, devlist[2].szIP, devlist[2].nKey);
printf("Item %u, IP: %s, Key: %u\n", 3, devlist[3].szIP, devlist[3].nKey);
// Now lets qsort by IP Address
qsort( (void *)devlist, (size_t)4, sizeof( mystruct ), IPcompare );
printf("Values after IP Address qsort\n");
printf("Item %u, IP: %s, Key: %u\n", 0, devlist[0].szIP, devlist[0].nKey);
printf("Item %u, IP: %s, Key: %u\n", 1, devlist[1].szIP, devlist[1].nKey);
printf("Item %u, IP: %s, Key: %u\n", 2, devlist[2].szIP, devlist[2].nKey);
printf("Item %u, IP: %s, Key: %u\n", 3, devlist[3].szIP, devlist[3].nKey);
const long nKey = 2L;
// Looking for IP by Key
printf("Looking for an IP address by key (DeviceID) - Search where nKey =
2\n");
mystruct* rr = (mystruct*) bsearch(&nKey, devlist, 4, sizeof(mystruct),
compare );
if( rr )
printf( "IP address: %s found (searched using key %u\n", rr->szIP,
rr->nKey );
else
printf( "IP Address not found!\n" );
printf("Now we look for a a key (DeviceID) by IP address - search on
192.168.1.3\n");
const char* szIP = "192.168.1.3";
// key, base search data, num, width, cmp_func
mystruct* fnd = (mystruct*) bsearch(&szIP, devlist, 4, sizeof(mystruct),
IPcompare ); // This line not working!!!
if( fnd )
printf( "nKey: %u found (searched using %s\n", fnd->nKey, fnd->szIP );
else
printf( "nKey NOT found!\n" );
return 0;
}
int compare( const void* val1, const void* val2 )
{
struct mystruct* sp1 = (mystruct*)val1;
struct mystruct *sp2 = (mystruct*)val2;
return (int)(sp1->nKey - sp2->nKey);
}
int IPcompare( const void* arg1, const void* arg2 ) // char **arg1, char
**arg2 )
{
/* Compare all of both strings: */
struct mystruct *sp1 = (mystruct*)arg1;
struct mystruct *sp2 = (mystruct*)arg2;
return _strcmpi( sp1->szIP, sp2->szIP );
}
The output I get is:
Values before qsort
Item 0, IP: 192.168.1.1, Key: 9
Item 1, IP: 192.168.1.2, Key: 2
Item 2, IP: 192.168.1.3, Key: 7
Item 3, IP: 192.168.1.4, Key: 1
Values after qsort
Item 0, IP: 192.168.1.4, Key: 1
Item 1, IP: 192.168.1.2, Key: 2
Item 2, IP: 192.168.1.3, Key: 7
Item 3, IP: 192.168.1.1, Key: 9
Values after IP Address qsort
Item 0, IP: 192.168.1.1, Key: 9
Item 1, IP: 192.168.1.2, Key: 2
Item 2, IP: 192.168.1.3, Key: 7
Item 3, IP: 192.168.1.4, Key: 1
Looking for an IP address by key (DeviceID) - Search where nKey = 2
IP address: 192.168.1.2 found (searched using key 2
Now we look for a a key (DeviceID) by IP address - search on 192.168.1.3
nKey NOT found!
Press any key to continue