(long)&((FLASH_CONF_STRUCT*)0)->vlan
FLASH_CONF_STRUCT
is a struct type, and vlan
is a member of this struct.
How to spell this expression?
(FLASH_CONF_STRUCT*)
is a type cast. It's casting 0
to a pointer that points to a FLASH_CONF_STRUCT
. Let's call this ptr
.
ptr->vlan
is equivalent to (*ptr).vlan
. It accesses the vlan
field of the structure pointed by ptr
.
&
gets the address of what follows, so the offset of vlan
added to ptr
.
(long)
casts the address to a long
.
Overall, this is meant to get the offset of vlan
within the structure. But I suspect it invokes Undefined Behaviour (because it dereferences a NULL pointer, at the very least). And it does so needlessly.
Replace
long ofs = (long)&((FLASH_CONF_STRUCT*)0)->vlan;
with the far more readable
#include <stddef.h>
size_t ofs = offsetof(FLASH_CONF_STRUCT, vlan);
For example,
#include <stddef.h>
#include <stdio.h>
typedef struct {
int a;
int b;
int vlan;
} FLASH_CONF_STRUCT;
int main(void) {
printf("%ld\n", (long)&((FLASH_CONF_STRUCT*)0)->vlan);
printf("%zu\n", offsetof(FLASH_CONF_STRUCT, vlan));
return 0;
}
Output:
$ gcc -Wall -Wextra -pedantic-errors -std=c99 a.c -o a && a
8
8