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 8580191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:46:30+00:00 2026-06-11T20:46:30+00:00

I have hash which is pointer to a struct. I’m trying to get it’s

  • 0

I have “hash” which is pointer to a struct. I’m trying to get it’s member stats which is also a pointer. I thought I could just do: hash->stats but that appears to return the reference stats struct. The “->” should just dereference the variable on the left?

struct statistics {
    unsigned long long count;   
   ...
};

struct hashtable {
    GHashTable * singleton; //Single Hash Table to Store Addresses
    struct statistics *stats;   //Statistics Table
};

    GHashTable *ghash = g_hash_table_new(NULL, NULL);
    struct hashtable *hash = (struct hashtable *) malloc(sizeof(struct hashtable));

//Works but why isn't hash->stats ok?
    memset(&hash->stats, 0, sizeof(struct statistics));

If I try this at this point:

struct statistics *st = hash->stats;

I get:

incompatible types when initializing type 'struct statistics *' using type 'struct 
     statistics'
  • 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-11T20:46:32+00:00Added an answer on June 11, 2026 at 8:46 pm

    Your line of code

     memset(&hash->stats, 0, sizeof(struct statistics));
    

    is plain wrong. The hash->stats is a pointer. Its size is 32 or 64 bits. When you take its address, like &hash->stats, the results is an address that points into the stucture, very close to its end.

    The call to memset clears the pointer field itself and the memory after it, that is right after the sturcture. You corrupt some memory in the heap. This will result either in undefined behavior or a crash. You should write something like:

       struct hashtable *hash = (struct hashtable*)malloc(sizeof(struct hashtable));
       struct statistics *stts = (struct statistics*)malloc(sizeof(struct statistics));
    
       hash->stats = stts;
       memset(hash->stats, 0, sizeof(struct statistics));
    

    This will init your data. Plus you need to free your memory when you are done with your data structure.

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

Sidebar

Related Questions

I have a ruby hash which looks like {10.1.1.6=>nick, 127.0.0.1=>nick1} But I can't manage
I have a hash in which I store the products a customer buys (
What I can think of is: Algo: Have a hash table which will store
I have a string which looks like a hash: { :key_a => { :key_1a
I have an object for which I want to generate a unique hash (override
In a project I'm writing code for, I have a void pointer, implementation, which
I have a hash in which I want to use the values as keys
I have a hash which looks like this: items: item: attribute_a: cheese attribute_b: bacon
I have a hash which has in excess of a thousand key/value pairs. I
I have a struct where a constant char string is defined and a pointer

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.