Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7165907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:19:39+00:00 2026-05-28T14:19:39+00:00

I have been trying to implement hash-tables using uthash.h, following the (excellent) documentation I

  • 0

I have been trying to implement hash-tables using uthash.h, following the (excellent) documentation I have been able to get the add and find functions to work with the following code:

#include <stdio.h>
#include "../src/uthash.h"

struct my_struct
{
    int id;                    /* key */
    char name[10];
    double height;
    UT_hash_handle hh;         /* makes this structure hashable */
};

/* hash-table declared as a global variable */
struct my_struct *users = NULL;    /* important! initialize to NULL */

void add_user(int user_id, char *name, double height) {
    struct my_struct *s;

    s = malloc(sizeof(struct my_struct));
    s->id = user_id;
    strcpy(s->name, name);
    s->height = height; 
    HASH_ADD_INT( users, id, s );  /* id: name of key field */
}

struct my_struct *find_user(int user_id) {
    struct my_struct *s;

    HASH_FIND_INT( users, &user_id, s );  /* s: output pointer */
    return s;
}

int main()
{  
    add_user(users, 1, "ben", 1.85);

    struct my_struct *user;
    user = find_user(users, 1);

    printf("user id: %d user name: %s height: %g\n", user->id, user->name,  user->height);

    return 0;
}

this was a good start, but I was unhappy with having the head of the hash declared as a global variable and would like to pass it in to the add and find functions as a parameter, e.g.:

#include <stdio.h>
#include "../src/uthash.h"

struct my_struct
{
    int id;                    /* key */
    char name[10];
    double height;
    UT_hash_handle hh;         /* makes this structure hashable */
};

void add_user(struct my_struct *users, int user_id, char *name, double height) {
    struct my_struct *s;

    s = malloc(sizeof(struct my_struct));
    s->id = user_id;
    strcpy(s->name, name);
    s->height = height; 
    HASH_ADD_INT( users, id, s );  /* id: name of key field */
}

struct my_struct *find_user(struct my_struct *users, int user_id) {
    struct my_struct *s;

    HASH_FIND_INT( users, &user_id, s );  /* s: output pointer */
    return s;
}

int main()
{
    /* hash_table declared as a local variable and passed in as a parameter to
     * add_user and find_user functions */
    struct my_struct *users = NULL;    /* important! initialize to NULL */

    add_user(users, 1, "ben", 1.85);

    struct my_struct *user;
    user = find_user(users, 1);

    printf("user id: %d user name: %s height: %g\n", user->id, user->name,  user->height);

    return 0;
}

this version is causing a SEG fault on the printf, it looks like nothing has been added to the hash table. can’t really see where the second version is going wrong. surely it can’t be a requirement to declare all your hash-tables as global variables to get it to work?

probably missed something obvious – but I can’t see what it is…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:19:39+00:00Added an answer on May 28, 2026 at 2:19 pm

    If you want to modify a pointer inside a function, you must use its address
    So, function

    add_user(struct my_struct *users, int user_id, char *name, double height)
    

    must be

    add_user(struct my_struct **users, int user_id, char *name, double height)
    

    if not, pointer is not modified.

    of, course, inside add_user use

    HASH_ADD_INT( *users, id, s )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to implement Win32's MessageBox using GTK. The app uses SDL/OpenGL,
I have been trying to implement pinch zoom/in-out for PhotoView (a UIImageView instance) using
I have been trying to implement the Authorize.NEt SIM Method using the information given
I've been trying to implement unit testing and currently have some code that does
I have been trying to find a really fast way to parse yyyy-mm-dd [hh:mm:ss]
I have been trying to get around this error for a day now and
I have been trying to implement a CheckBox Node Tree, where the parent nodes
I have been trying to implement Nhibernate.Linq 1.0.0.4000 together with Nhibernate 2.1.2.4000. After what
OK. So I have been trying to implement a timer. Now a very weird
I am a beginner interested in Haskell, and I have been trying to implement

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.