Like Tree1Likes
  • 1 Post By Salem

uthash library - string key not working

This is a discussion on uthash library - string key not working within the C Programming forums, part of the General Programming Boards category; hi, is anyone using this library?? I have a problem : This chunk of code does not work : Code: ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    172

    uthash library - string key not working

    hi, is anyone using this library??


    I have a problem :


    This chunk of code does not work :
    Code:
    #include <string.h>  /* strcpy */
    #include <stdlib.h>  /* malloc */
    #include <stdio.h>   /* printf */
    #include "uthash.h"
    
    typedef struct hashTableChrInt {
        char key[500];                    /* key */
        int value;             
        UT_hash_handle hh;         /* makes this structure hashable */
    }hashTableChrInt;
    
    
    void add_read( hashTableChrInt * reads,  char *read) {
    	hashTableChrInt *s = NULL;;
        int x =10;
     
    	s = ( hashTableChrInt*)malloc(sizeof( hashTableChrInt));
        strcpy(s->key, read);
        s->value = x;
        HASH_ADD_STR( reads, key, s );
        
    
    }
    
    int main() {
         char read[] = "Robert";
         char name[]="betty";
         hashTableChrInt *s, *tmp, *reads = NULL;
         
    add_read(reads,read);
    add_read(reads,name);
    
    
        HASH_FIND_STR( reads, "betty", s);
        if (s) printf("betty's id is %d\n", s->value);
    
        /* free the hash table contents */
        HASH_ITER(hh, reads, s, tmp) {
          HASH_DEL(reads, s);
          free(s);
        }
        return 0;
    }

    But this one does :


    Code:
    #include <string.h>  /* strcpy */
    #include <stdlib.h>  /* malloc */
    #include <stdio.h>   /* printf */
    #include "uthash.h"
    
    typedef struct hashTableChrInt{
        char key[500];             /* key (string is WITHIN the structure) */
        int value;
        UT_hash_handle hh;         /* makes this structure hashable */
    }hashTableChrInt;
    
    
    int main() {
         char read[] = "Robert";
         char name[]="betty";
         hashTableChrInt *s, *tmp, *reads = NULL;
          int i=2;
    
    
            s = ( hashTableChrInt*)malloc(sizeof(hashTableChrInt));
            strcpy(s->key, read);
            s->value = i++;
            HASH_ADD_STR( reads, key, s );
            s = ( hashTableChrInt*)malloc(sizeof( hashTableChrInt));
            strcpy(s->key, name);
            s->value = i++;
            HASH_ADD_STR( reads, key, s );
    
        HASH_FIND_STR( reads, "betty", s);
        if (s) printf("betty's id is %d\n", s->value);
    
        /* free the hash table contents */
        HASH_ITER(hh, reads, s, tmp) {
          HASH_DEL(reads, s);
          free(s);
        }
        return 0;
    }
    where is my making mistake ?? The only thing i did is to outsource my adding into a function.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    hashTableChrInt *s, *tmp, *reads = NULL;
    ...
    HASH_ADD_STR( reads, key, s );
    ...
    HASH_ADD_STR( reads, key, s );

    Being a macro, it seems that it modifies reads (otherwise what would be the point).

    Since the pointer is modified, you need to update the actual pointer in main, not the temporary parameter copy in your function.
    baxy likes this.
    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
    Join Date
    Jan 2011
    Posts
    172
    thnx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reverse a string without string.h library
    By antros48 in forum C Programming
    Replies: 6
    Last Post: 09-10-2011, 06:01 PM
  2. Help - rewriting style of string library in C
    By ~C_Student~ in forum C Programming
    Replies: 24
    Last Post: 11-19-2009, 08:34 PM
  3. Simply Library Catalog Program not working
    By Bakster in forum C Programming
    Replies: 17
    Last Post: 09-04-2009, 08:06 PM
  4. Understanding strtok from string.h library
    By yougene in forum C Programming
    Replies: 6
    Last Post: 03-07-2008, 12:14 AM
  5. working on a string tough string prog, need help
    By Brokn in forum C Programming
    Replies: 1
    Last Post: 11-13-2005, 03:14 PM

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