Search code examples
cpointersstructuresizeofdereference

what does this line of code "#define LIBINJECTION_SQLI_TOKEN_SIZE sizeof(((stoken_t*)(0))->val)" do?


In particular I'd like to know what ->val does in the

sizeof(((stoken_t*)(0))->val)

and what stoken_t*(0) pointer do, in particular what the (0) means?

I hope I have formulated my question clearly enough.


Solution

  • This is a way of accessing a member of a structure at compile time, without needing to have a variable defined of that structure type.

    The cast (stoken_t*) to a value of 0 emulates a pointer of that structure type, allowing you to make use of the -> operator on that, just like you would use it on a pointer variable of that type.

    To add, as sizeof is a compile time operator, the expression is not evaluated at run-time, so unlike other cases, here there is no null-pointer dereference happening.

    It is analogous to something like

    stoken_t * ptr;
    sizeof(ptr->val);