简体   繁体   中英

Arithmetic of the double pointers with a data structure

i tryied to write the hashtable and found this github repository: enter link description here . I'm having difficulty understanding this code:

struct entry_s
{
    char* key;
    char* value;
    struct entry_s* next;
};

typedef struct entry_s entry_t;

struct hashtable_s 
{
    int size;
    struct entry_s** table;
};

typedef struct hashtable_s hashtable_t;

I've two questions:

1) Why there using the typedef struct entry_s entry_t; instead of

struct entry_s
{
    char* key;
    char* value;
    struct entry_s* next;
};entry_t; 

because if i use second way i will have error.

2) What does that code mean: struct entry_s** table;

I know this question can be so eazy to answer, but I will be glad if you help me

Why there using typedef struct entry_s entry_t; instead of

struct entry_s { char* key; char* value; struct entry_s* next; };entry_t;

The syntax you give is not correct and does not do what you think. The last line, };entry_t; , is regarded as a new statement, because your compiler expects that after each ; there will be a new one. What you want to write is the following code:

struct entry_s
{
    char* key;
    char* value;
    struct entry_s* next;
} entry_t;

This structure is a linked list, you can see that one of the fields (the last one) uses the same structure to keep a data. In a more abstract way, you can see this code such as:

struct my_linked_list_s
{
    T data;
    struct my_linked_list_s *next;
};

In C, a recursive data type (ie one that is used in its own definition), requires a pointer (for reasons of measuring the size of the structure*), which is why we see one in the next field. Then, in order to distinguish between the "internal" use of the structure and its external use, we define an alias type:

typedef struct my_linked_list_s my_linked_list_t;

Or, in your case:

typedef struct entry_s entry_t;

This is not a necessary thing though, but your implementation has chosen to do so. You can see the naming convention: _s for "structure" and _t for "type".

Because if i use second way i will have error.

You should read your compiler's error messages more carefully, it would have helped you understand what's wrong.

What does that code mean: struct entry_s** table;

This is an array of pointers. Your implementation has chosen to use linked lists and pointers, this function clearly describes the adding part.

For a better overview of the C hash tables, check out this answer .

*To determine the size of a structure ( sizeof(struct my_struct) ), the sum of the size of all fields is used. If we were to determine the size of a recursive structure without using a pointer (a pointer has a fixed size, for any type of data), then the structure would have an infinite size. To avoid this problem, we therefore use a pointer.

The answer for the second question is you are decalring an array of pointer that can store the pointers of the type struct entry_s** .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM