Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help sending multiple structure types to a single function

Status
Not open for further replies.

bloggins02

Programmer
Jul 9, 2000
5
US
Hi everyone :)<br><br>Bear with me here I'm teaching myself C as I go.&nbsp;&nbsp;I would like to be able to send different types of structs accross a network using the same function.&nbsp;&nbsp;I don't have a problem breaking the structs down and send()ing them, but I tried this and it doesn't work:<br><br>int send_struct(int fd, void *input_struct, int struct_type)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switch (struct_type) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case THIS_TYPE_OF_STRUCT:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;do_this(fd, (struct this_type) <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;input_struct-&gt;member);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case THAT_TYPE_OF_STRUCT:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;do_that(fd, (struct that_type)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;input_struct-&gt;member);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br>}<br><br>Of course when I compile this little &quot;gem of brilliant coding&quot; I get a &quot;member is not part of a structure or union&quot; and I also get &quot;warning: attempt to dereference a void pointer&quot;.&nbsp;&nbsp;I know I've got the all wrong here.&nbsp;&nbsp;What do I need to do to implement this?&nbsp;&nbsp;Is it even possible?<br><br>Thanks a lot guys!
 
&gt; do_this(fd, (struct this_type) <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;input_struct-&gt;member);<br><br><br>Should be:<br><br>do_this(fd, ((struct this_type)input_struct)-&gt;member);<br><br>Good luck<br>-pete
 
Thanks!&nbsp;&nbsp;I think I get it.&nbsp;&nbsp;The reason is because if you cast it like:&nbsp;&nbsp;(struct this_struct)input_struct-&gt;member then what that is saying is &quot;cast the object member to the type this_struct&quot; but since input_struct is a void * by default then the compiler complains that input_struct is not a struct, so you have to do ((struct this_struct)input_struct)-&gt;member to tell the compiler to cast this_struct to input_struct and not to it's &quot;member&quot; (ahem).<br><br>Do I have this right?&nbsp;&nbsp;I'm still learning that's why I'm kinda thinking out loud here.
 
Dear bloggins02,<br><br>First, my post was incorrect it should actually be:<br><br>do_this(fd, ((struct this_type*)input_struct)-&gt;member);<br><br>Second, you are sort of right but it is simpler. It's the associative properties of the statement evaluation. If you do this:<br><br>(struct this_type*)input_struct-&gt;member;<br><br>Then the result is this:<br>(struct this_type*)(input_struct-&gt;member);<br><br>Therefore this will be the first part of the expression that is evaluated:<br>(input_struct-&gt;member);<br>and since input_struct is of type void* it has no members, so by the expression:<br><br>((struct this_type*)input_struct)-&gt;member;<br>you implicitly supply the evaluation order of the expression so that this will be evaluated first:<br>((struct this_type*)input_struct)<br><br>The equivalent would be this in two steps:<br>struct this_type* ptr = (struct this_type*)input_struct;<br>do_this(fd,ptr-&gt;member);<br><br>Hope this helps<br>-pete<br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top