0

I have read this article: Quick Way to Implement Dictionary in C and implemented it. Need to say that I change the install and lookup to use hashtab as a argument instead of a global variable, It worked like charm:

struct nlist *install(struct nlist **hashtab,char *name, symbol *dfn);
struct nlist *lookup(struct nlist **hashtab, char *s);

The problem is, I need to copy the hash into a new one. I first try:


struct nlist **copy_context(struct nlist **hashtab){
    nlist **copy = (nlist**)malloc(sizeof(nlist*)*HASHSIZE);
    struct nlist *np;
    int i = 0;
    for (np = hashtab[i]; i<HASHSIZE ;i++ ){
        debug_log("i vale %i",i);
        if( np != NULL ){
            install(copy,np->name,np->dfn);
        }
    }
    return copy;
}

but is not clear to me where the values are stored neither where to start the copy. I was thinking something like:

struct nlist **copy_context(struct nlist **hashtab){
nlist **copy = (nlist**)malloc(sizeof(nlist*)*HASHSIZE);
i = 0;//this is not correct
or (np = hashtab[i]; np != NULL; np = np->next){
        install(copy,np->name,np->dfn);
    }
return copy;
}

but is notingh is working.

daaan
  • 47
  • 5

1 Answers1

0

as "Some Programmer dude" said I need to do two loops, one on the table itself and one for the list in that table index.

struct nlist **copy_context(struct nlist **hashtab){
    nlist **copy = (nlist**)malloc(sizeof(nlist*)*HASHSIZE);
    struct nlist *np;
    for (int i = 0; i<HASHSIZE; i++ ){
        np = hashtab[i];
        while( np )
        {
            install(copy,np->name,np->dfn);
            np = np->next;
        }
    }
    return copy;
}
daaan
  • 47
  • 5