LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

ListFindItem using CStringCompare

I am trying to use ListFindItem to search a List for a string. 
 
Using ListFindItem (List_Watch, car_number, FRONT_OF_LIST, 0);  will not find a match that I know is there.
 
THen I tried using ListFindItem (List_Watch, car_number, FRONT_OF_LIST, CStringCompare); but got the following error Fatal Run-Time Error 'General Protection Fault'
 
I do not know if I am formatting the CStringCompare call correctly and can not find example.
 
I really do not know while it is not working with the original attemp of just using"0".  The string I am comparing is "63"  loaded into car_number (char car_number[10];)  and I know it is in List_Watch because I also laod List_Watch into a List Ring and I can see the string 63 in it!
 
 
0 Kudos
Message 1 of 3
(3,265 Views)
Hello altronics,

So regarding your two questions, so instead of passing 0 as the compare function in ListFindItem (which causes CVI to do a memcpy on the two strings), you can pass the CStringCompare function which will perform a strcmp comparison on the two strings.

The reason why you are getting a fatal run-time error when you pass CStringCompare as the compare function is most likely because you are not passing a null terminated string or a large enough array into the Item to Find parameter of the ListFindItem function. 

So I wrote up some sample code, which works on my end.  It should help you reformulate how you are calling the ListFindItem function.  Hope it helps.

#include "toolbox.h"

int main (int argc, char *argv[])
{

    char *dog = "dog";
    char *cat = "cat";
    int result;
    ListType newList;
   
    char *itemToFind = "cat";

    newList = ListCreate(sizeof(char *));

    if (newList)
    {
        ListInsertItem(newList, &dog, END_OF_LIST);
        ListInsertItem(newList, &cat, END_OF_LIST);
    }

    result = ListFindItem (newList, &itemToFind, FRONT_OF_LIST, CStringCompare); 
    return 0;
}
Wendy L
LabWindows/CVI Developer Newsletter
0 Kudos
Message 2 of 3
(3,219 Views)

Hello,

if you pass an array to the functions, you will get this error. It must be a pointer.

 

Regards

Frank_deSmiley Happy 

 

0 Kudos
Message 3 of 3
(3,185 Views)