2

So I am looking at this and the guy is continuously using

uint32_t *internalNodeNumKeys(void *node)
{
    return (uint32_t *)(node + INTERNAL_NODE_NUM_KEYS_OFFSET);
}

*internalNodeNumKeys(root) = 1;

What does this do? Somewhere I remember him saying that because these functions return pointers they can be used as setters but what do they set?

1 Answers1

2

The function returns a pointer to int, the expression *internalNodeNumKeys(root) = 1; is parsed as:

*(internalNodeNumKeys(root)) = 1;

Postfix operators such as () for function call bind more tightly than prefix operators such as * for dereferencing.

Note also that internalNodeNumKeys is highly non portable:

  • Performing pointer arithmetics on void pointers is non portable, node should be cast as (unsigned char *) before the addition.
  • Casting an arbitrary offset into the object pointed to by node as a pointer to int may have undefined behavior, the programmer is playing with fire at this point.
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • Yes, but as it has been addressed in one of the comments to the question by Jamesdlin, `node + INTERNAL_NODE_NUM_KEYS_OFFSET` is illegal, as it is doing pointer arithmetic with a `void *`. It should be better to have declared the parameter as `char *` instead. – Luis Colorado Feb 05 '19 at 04:53