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

  • Home
  • SEARCH
  • 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 8655965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:09:48+00:00 2026-06-12T15:09:48+00:00

When I my function, my hash is empty. Why? Here is my code: #include

  • 0

When I my function, my hash is empty. Why?

Here is my code:

#include <stdio.h>
#include <string.h>
#include "uthash.h"
struct oid_struct {
  char descr[20];
  char oid[50];
  UT_hash_handle hh;
};

testadd( struct oid_struct* oid_hash){

 struct oid_struct *element;
 element=(struct oid_struct*) malloc(sizeof(struct oid_struct));

 strcpy(element->descr, "foo");
 strcpy(element->oid, "1.2.1.34");
 HASH_ADD_STR(oid_hash, descr, element);
 printf("Hash has %d entries\n",HASH_COUNT(oid_hash));

}


main(){
        struct oid_struct *oid_hash = NULL, *lookup;
        testadd(oid_hash);
        printf("Hash has %d entries\n",HASH_COUNT(oid_hash));

}

Here is the output:

# gcc hashtest.c
# ./a.out
Hash has 1 entries
Hash has 0 entries
#
  • 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-06-12T15:09:49+00:00Added an answer on June 12, 2026 at 3:09 pm

    C passes arguments by value, meaning a copy of oid_hash is being changed inside testadd() so the change is invisible to the caller. Pass the address of oid_hash to testadd():

    testadd(&oid_hash);
    
    void testadd(struct oid_struct** oid_hash)
    {
        *oid_hash = element; /* Depending on what is going on
                                inside HASH_ADD_STR(). */
    }
    

    Note the casting of return value of malloc() is not required.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following C++ code: #include <iostream> #include <google/dense_hash_map> #include <string.h> using google::dense_hash_map;
I have gone through this page... http://www.php.net/manual/en/function.hash.php MD5 is 32 characters long while sha1
Is there a hash function that returns a 16-digit hex value (as MD5 returns
Possible Duplicate: Is there any hash function in PL/SQL? I have a column with
I'm looking for a hash function that: Hashes textual strings well (e.g. few collisions)
What is the best 32bit hash function for relatively short strings? Strings are tag
I have to create a hash function based on 3 shorts. What is the
When implementing a hash table using a good hash function (one where the probability
I have to write my own hash function. If I wanted to just make
Can someone please point me out a hash function (preferably an implementation) which would

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.