Academic Company Events NI Developer Zone Support Solutions Products & Services Contact NI MyNI
This Document is not yet Rated  Rate this Document

CStringCompare Returns An Error When Comparing Two Identical Strings in LabWindows/CVI

Primary Software: LabWindows/CVI Development Systems>>Full Development System
Primary Software Version: 8.0
Primary Software Fixed Version: N/A
Secondary Software: LabWindows/CVI Development Systems>>Base Package

Problem:
I am trying to use the ListFindItem function in LabWindows/CVI to search for a string in a List. In particular, I am using CStringCompare as the search function. The ListFindItem function fails to find strings that are present in my List. Why am I seeing this incorrect behavior, and how can I correct it?

Solution:
The CStringCompare function will perform a strcmp comparison on your search string and on each string in your List. Due to how the search string is dereferenced and passed to the strcmp function, your search string must be a pointer instead of an array of characters for the ListFindItem function to return correct results.

An example of how to use the ListFindItem and CStringCompare functions is shown below:

#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;
}


Note: If you pass an array of characters as your search string, the ListFindItem function will either not find the string in your List, or it will thrown a General Protection Fault fatal run-time error.

Related Links:

Attachments:





Report Date: 03/07/2006
Last Updated: 03/08/2006
Document ID: 3V6FBIK7

Your Feedback! poor Poor  |  Excellent excellent   Yes No
 Document Quality? 
 Answered Your Question? 
  1 2 3 4 5
Please Contact NI for all product and support inquiries.submit