using memchr()

This is a discussion on using memchr() within the C++ Programming forums, part of the General Programming Boards category; Below is the code I made using memchr(); the problem is that it returns true only if the first when ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 10: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21