Note that there are some explanatory texts on larger screens.

plurals
  1. POprepend and remove from a ( void * ) in C
    text
    copied!<p>i think this is a pretty straight forward problem , but i still can not figure it out . </p> <p>I have function which sends stream over the network . naturally , this takes const void * as argument:</p> <pre><code>void network_send(const void* data, long data_length) </code></pre> <p>i am trying to prepend a specific header in the form of char* to this before sending it out over the socket:</p> <pre><code> long sent_size = strlen(header)+data_length; data_to_send = malloc(sent_size); memcpy(data_to_send,header,strlen(header)); /*first copy the header*/ memcpy((char*)data_to_send+strlen(header),data,dat_length); /*now copy the actual data*/ </code></pre> <p>This works fine as long as the data is actually char* . but if it changes to some other data type , then this stops working . </p> <p>when receiving , i need to remove the header from the data before processing it . so this is how it do it:</p> <pre><code>void network_data_received(const void* data, long data_length) { ........ memmove(data_from_network,(char*)data_from_network + strlen(header),data_length); /*move the data to the beginning of the array*/ ProcessFurther(data_from_network ,data_length - strlen(header)) /*data_length - strlen(header) causes the function ProcessFurther to read only certain part of the array*/ } </code></pre> <p>This again works ok if the data is char type . but crashes if it is of any different type . </p> <p>Can anyone suggest how to properly implement this ?</p> <p>Regards, Khan</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload