Thread: working with void pointers

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    31

    working with void pointers

    I have the following functions :

    Code:
    typedef struct
    {
    char authors[5][40]; // first and last names of the author
    char title[150]; // title of the publication
    unsigned int year; // year of the publication
    char isbn[15]; // publication code
    } PubEntryT;
    
    
    NodeST *findS(ListST *L, void *pKey, void * (*itemKey)(void *pItem), int(*keyCmp)(void *pKey1, void *pKey2))
    // returns a pointer to the cell having a key equal to the one pointed to by pKey – if such a node exists
    // on the list – or NULL if it cannot find such a node.
    {
      NodeST *p;
    
      if (L != NULL)
       {
       if (L->first != NULL)
        {
          p = L->first;
         while ( p != NULL)
          {
    
            if ( keyCmp ( itemKey (p->Item),pKey) == 0)
                  break;
             p=p->next;
          }
        }
        else {
               //errorSmessage(2);
               return NULL;
             }
      }
      return p;
    }
    
    
    void *itemKeyIsbn(void *pItem)
    { // for returning the isbn field of a book record
    return ((PubEntryT *) pItem)->isbn;
    }
    
    int keyCmpStr(void *pKey1, void *pKey2)
    { // comparison of two ASCIIZ string keys; good for author or isbn comparison
    return strcmp((char *)pKey1, (char *)pKey2);
    }
    I have to call this functions to find the node with a given key (a certain isbn, for example) and I use this call:

    p = findS(L, pItem->Item,itemKeyIsbn(pItem),keyCmpStr(pItem->Item, pItem->Item)));
    I'm not sure that my call is correct. In fact, I get segmentation fault in findS at the line
    Code:
     
    ( keyCmp ( itemKey (p->Item),pKey) == 0)

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your findS function can return garbage if L is NULL, since it will return an uninitialized p. You do need to make sure that the pItem in your main function exists, because if you try to call pItem->Item when pItem is NULL (or uninitialized), then your program will die.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    Code:
    /* Did you realize that this line has one too many )'s?  */
    p = findS( L, pItem->Item,  itemKeyIsbn(pItem), keyCmpStr( pItem->Item , pItem->Item)) );
    Code:
    /* Shouldn't this: */
    if ( keyCmp( itemKey( p->Item) , pKey ) == 0)
    
    /* be this: */
    if ( (keyCmp( itemKey( p->Item) , pKey) ) == 0)
    What is ListST , NodeST , pItem ???
    Too many missing pieces here.
    You should have posted a minimal running example that demonstrates the segfault.

    Try to trace and pinpoint the exact location of the segfault by printing pertainant variable values.
    If you are using linux follow any such print with a fflush(stdout); to be sure you get the output before segfault stops it.
    Last edited by HowardL; 03-30-2010 at 09:54 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I didn't even look at the bit that wasn't in tags. That call should actually be
    Code:
    p = findS(L, pItem->Item, itemKeyIsbn, keyCmpStr);
    You don't try to call a function when you want to pass a function name.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    Oh I don't know , isn't findS() expecting pointer values to be returned by the functions in it's arguments:
    Code:
    NodeST * findS( ListST * L,
                     void * pKey,
                     void * (*itemKey)(void *pItem) ,
                      int (*keyCmp)(void *pKey1 , void *pKey2)
                 )
    Last edited by HowardL; 03-31-2010 at 12:11 AM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by HowardL View Post
    Oh I don't know , isn't findS() expecting pointer values to be returned by the functions in it's arguments:
    Well, yes. But this function is not expecting an int* or a void * to be passed in, it's expecting a function so that the functions can be called by the function itself.

  7. #7
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    oh, I see: void * (*itemKey)(void *pItem) is a pointer to function.
    I'll shut up now , sorry to confuse things.
    Last edited by HowardL; 03-31-2010 at 12:13 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. How to better manage large .cpp files
    By 39ster in forum C++ Programming
    Replies: 6
    Last Post: 08-25-2008, 08:24 AM
  4. ChangeDisplaySettings - Blank?
    By Tonto in forum Windows Programming
    Replies: 13
    Last Post: 12-26-2006, 04:17 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM