Thread: using memchr()

  1. #1
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572

    using memchr()

    Below is the code I made using memchr(); the problem is that it returns true only if the first when asked to search the first element, ie:
    Code:
    setArray[] = { 1 2 3 };
    and then I search for x=1 everything is fine
    but when searching for x=2 or x=3 it returns false
    Code:
    void Sets::membership( int x, int set )
    {
    	int size = (allSets[set]->size);
    	if( memchr( allSets[set]->setArray, x, size ) == NULL ) 
    		cout << "   No" << endl;
    	else
    		cout << "   Yes" << endl;
    
    }
    any ideas?

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Unless your array is a char array, it isn't going to work
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    >>Unless your array is a char array, it isn't going to work

    then why does it work for the element in the first position?

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. running time for memchr()
    By axon in forum C++ Programming
    Replies: 5
    Last Post: 12-04-2003, 11:50 AM
  2. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM