What does this mean ? - (t_object **)&link

Ian C's icon

When looking up contents of a hashtab, I'm using this Max API method to check a hashtab key for a the presence of a linklist.

t_linklist *link = NULL;
hashtab_lookup(x->l_hash_note_offs, key, (t_object **)&link);

This works fine (code was provided by a generous Dev forum contributor). However, I'd love it if someone could help me translate this snippet...

(t_object **)&link

I've looked through my C reference materials and can cautiously say that &link is the address of a linklist, and that we're casting "t_object" (right?), but I don't get the interplay between the double dereference operator (**), "t_object" and the "&link." What's going on there?

Thanks!

Ian

kLSDiz's icon

&link is the address of the pointer to linklist, so hash_lookup function can provide (and write somewhere) the pointer to an object (linklist). Old good C.

Ian C's icon

Thanks, KLSDIZ!

To refine the question further, what is the (t_object **) doing exactly? From the docs, I can see that a "_linklist" contains a "t_object," but why the double pointer? Does the double pointer (a pointer to a pointer) reach for something inside the "object" struct?

I suppose you could say I'm having trouble using language to describe how pointers and structs interact. I'm new to C coding and still trying for form a mental model (language) for how all this stuff works.

Any additional clarity is appreciated. Thanks again!

Ian

kLSDiz's icon

Actually it's quite simple. something** is a type of pointer to a pointer to some stuff in memory, in other words an address of memory location which contains an address (as a numerical value) to some other memory location, where some data sit. You can provide this address (of the pointer) to a function to put there (at this address) another address (numerical value), this time of the memory location of asked asset. When this function returns you get two things: one is a return value from the function (in most cases it's some error code, so you can act if things go wrong) and the updated pointer to asked resource (so now 'link' pointer points to different place in memory than before calling 'hash_lookup', assuming there was no error – things went well).

Ian C's icon

Thank you for the response, KLSDIZ!