I am using uthash (http://uthash.sourceforge.net/userguide.html) to use a Hash Table implementation in my C Program.
I am trying to printf the data that is being added to the hash table before and after the addition and I get garbage values when I print it after the malloc. Please see the following code.
void add_user(int user_id, char *name)
{
printf("User ID : %d Name : %s, user_id,name); // Prints fine !!
struct my_struct *s;
s = malloc(sizeof(struct my_struct));
s->id = user_id;
strcpy(s->name, name);
printf("User ID : %d Name : %s, s->user_id,s->name); // Prints User ID fine, but for Name, only half of it is printed right, rest is Garbage !!
HASH_ADD_INT( users, id, s );
}
If I do the following :
void add_user(int user_id, char *name)
{
printf("User ID : %d Name : %s, user_id,name); // Prints fine !!
struct my_struct *s;
s = malloc(sizeof(struct my_struct));
printf("User ID : %d Name : %s, user_id,name); // Printing same as previous printf then I get Garbage values printed for Name
}
BUT if I comment out malloc as well, then both printf statements print correctly.
HELP !!!