SevO
SevO

Reputation: 303

Pointer to element of array pointers

I am working on a project, where i have problem with understanding the logic. I would appreciate i someone could explain it to me or make it at least more clear. Data structure is hash table if it changes something.

Code:

typedef struct tHTElem {
    char* key;
    int data;
    struct tHTElem* ptrnext;
} tHTElem;  

typedef tHTElem* tHT[MAX_SIZE];  

As long as i understand this, tHT[] is array of pointers to structure tHTElem ?
So if i want to create pointer to one element of this array, i should create it like this ?

tHTElem *ptrToElem = NULL;  

And initialize it like this ?

ptrToElem = tHT[42];  

I am kinda lost in this..
Thank you for any advice.

Upvotes: 1

Views: 73

Answers (1)

Felipe Soares
Felipe Soares

Reputation: 121

You do not need the typedef in the line typedef tHTElem* tHT[MAX_SIZE]; Because when you declared the struct you already used the typedef statement. Since then, you only need to use the type name, which is tHTElem.

No matter the type, when you declare an array, you are declaring a pointer. So this: char **c is equal to this: char * c[], it means c is a pointer to somewhere in the memory where there is a pointer to an array.

In your case you have a tHTElem* tHT[MAX_SIZE];, which is the same as tHTElem** tHT;. So if you want to create a poiter to the element 42 you should do like this:

tHTElem *ptrToElem;
ptrToElem = *tHT[42];

The * in the ... = *tHT[42]; is for you to access the memory where the array is and the 42 is the position of it.

Upvotes: 2

Related Questions