guys,
I am implementing hash function in below code snippet but unfortunetly getting segmentation fault. Could you please help me to understand that what went wrong in this implementation.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
unsigned long hash(char** str )
{
unsigned long hash_val = 5381;
unsigned long sum=0;
char* val ;
int i,j;
for ( j=0;j<9;j++)
{ val=malloc(strlen(str[j])+1);
memset(val,'\0',strlen(str[j])+1);
strcpy(val,str[j]);
for (i=0;val[i]!='\0';i++ )
{
sum=sum+val[i];
}
printf("%ld",sum);
return sum;
}
}
int main()
{
int i;
char *str[9]={"mohan","ramesh","mohan","ramesh","cat","act","manish","vashsh","dfjdsj"};
unsigned long key;
for(i=0;i<9;i++)
{
key= hash(str[i]);
printf("\nkey[%ld]:val[%s]",key,str[i]);
}
return 1;
}