typedef struct _node{
char *name;
char *desc;
struct _node *next;
}node;
#define HASHSIZE 101
static node* hashtab[HASHSIZE];
char* m_strdup(char *o){ // the m_strdup function.......
int l=strlen(o)+1;
char *ns=(char*)malloc(l*sizeof(char));
strcpy(ns,o);
if(ns==NULL)
return NULL;
else
return ns;
}
int install(char* name,char* desc){
unsigned int hi;
node* np;
if((np=lookup(name))==NULL){
hi=hash(name);
np=(node*)malloc(sizeof(node));
if(np==NULL)
return 0;
np->name=m_strdup(name); // this assignment.......why this way?????
if(np->name==NULL) return 0;
np->next=hashtab[hi];
hashtab[hi]=np;
}
else
free(np->desc);
np->desc=m_strdup(desc); //same question........
if(np->desc==NULL) return 0;
return 1;
}
main(){
char* names[]={"name","address","phone","k101","k110"};
char* descs[]={"Sourav","Sinagor","26300788","Value1","Value2"};
inithashtab();
install(names[0],descs[0]; // this call....
install(names[1],descs[1]);
printf("Done");
}
my question is why can't we initialize np->name=name directly ? instead of copying name into another location (using m_strdup) function ..... i have tried it but np->name=name doesnot work ........any idea is appreciated......... the parts of the code to be concentrated are shown using using comments