typecasting from one struct to another

I've been reading through a book and see this typecast a lot:
[code]
struct ip *pip = (struct ip *) pep->ep_data;
[/code]
What I don't understand is how this works. The two structures are completely different. What I'm wondering is how this turns out. For instance, what would the members of the structure pip hold based on what pep holds.

The structs are as follows:
[code]
struct ip {
u_char ip_verlen;
u_char ip_tos;
u_short ip_len;
short ip_id;
short ip_fragoff;
u_char ip_ttl;
u_char ip_proto;
short ip_cksum;
IPaddr ip_src;
IPaddr ip_dst;
u_char ip_data[1];
};

struct ep {
u_long ep_nexthop;
short ep_ifn;
short ep_len;
short ep_order;
struct eh ep_eh;
char ep_data[EP_DLEN];
};
[/code]

Comments

  • The ep structure has a member named ep_data which is a character array. The conversion you mentioned just converts the contents of it into a structure. It's not a conversion between two structures.
  • : The ep structure has a member named ep_data which is a character array. The conversion you mentioned just converts the contents of it into a structure. It's not a conversion between two structures.
    :
    So what you're saying is the character array will hold the contents of the entire ip struct?
  • The structure with the character array is a general ethernet header at first glance. There are different protocols based on ethernet (IPX for example) with different formats. IP is one of them. If you want to define a general structure for ethernet frames you create a character array with the size of the maximum bytes an ethernet frame can hold. Then for each protocol define a structure and cast your character array to that type. The character array is just a placeholder.
  • : The structure with the character array is a general ethernet header at first glance. There are different protocols based on ethernet (IPX for example) with different formats. IP is one of them. If you want to define a general structure for ethernet frames you create a character array with the size of the maximum bytes an ethernet frame can hold. Then for each protocol define a structure and cast your character array to that type. The character array is just a placeholder.
    :
    Got it. Thanks
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion