It’s assigning the address of sendbuf
to eh
as though sendbuf
were an object of type struct ether_header
.
There are two reasons to do this:
- In C, you cannot assign a pointer value of one type to a pointer of a different type without an explicit cast, unless one of the pointers has type
void *
. You cannot assign a char *
value to a struct ether_header *
object without a cast.
- A common (unsafe) practice is to map
struct
types onto arrays of char
or unsigned char
by creating a pointer like this. You can set bytes in the array by accessing members of the struct type likeeh->ether_type = 0x0800;
. Then you just send the array over a connection using fwrite
or something.
It’s unsafe because it’s not guaranteed elements of the struct type will be aligned correctly, and strictly speaking the behavior is undefined.