Different string comparison question

This is a discussion on Different string comparison question within the C Programming forums, part of the General Programming Boards category; I cannot seem to match up the strings when looking for them in my SymTab. I know the hash function ...

  1. #1
    Registered User P3nGu1N's Avatar
    Join Date
    Feb 2009
    Location
    La Crescent, MN
    Posts
    23

    Different string comparison question

    I cannot seem to match up the strings when looking for them in my SymTab. I know the hash function works and will always hash the name to the correct location, so it must be something semantic I'm doing when trying to compare the strings. Please help.

    Code:
    struct SymEntry* FindName(struct SymTab *ATable,
                               const char * Name) {
    									
    		printf("\nFinding Name: \"%s\" ", Name);
    		int comp;
    		int hashValue = hash(Name,ATable);
    
    		char n[200];
    		int nameLength;
    		struct SymEntry *currentEntry;
    		
    		strcpy(n, Name);
    		nameLength = strlen(n) + 1;
    		
    		if(ATable->Contents[hashValue] == NULL){
    			printf("\nATable->Contents at hashValue were NULL, returning NULL\n");
    			return NULL;
    		} /*else */
    		for(currentEntry = ATable->Contents[hashValue]; 
    			currentEntry->Next != NULL; currentEntry = currentEntry->Next) {
    			printf("\ncurrentEntry = %s\n",currentEntry->Name);
    			printf("\ncomparing n = %s to currentEntry = %s\n", n , currentEntry->Name);  
    			comp = strcmp( n, currentEntry->Name);
    			printf("\ncomp = %d",comp);
    			if (comp == 0){
    				return currentEntry;
    			}
    		}
    		return NULL; /* wasn't found in the list at the hash value */
    };
    the printf's are just for debugging, sorry if it makes a more jumbled code.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    if currentEntry->Next == NULL
    you do not enter the loop, and do not check the name,

    replace the for loop condition to check currentEntry != NULL
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User P3nGu1N's Avatar
    Join Date
    Feb 2009
    Location
    La Crescent, MN
    Posts
    23
    good call, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  3. Next Question...
    By Azmeos in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2003, 03:40 PM
  4. String array question
    By gogo in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 06:44 PM

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