1

I am trying to send a msg from a socket that will be in the form on

double,int,int,int,int,...,int

N int values how can i send it? i have opened a socket but how can i put all those elements in one array that will be sent in:

status=sendto(SendSocket,msg,sizeof(double)+N*sizeof(int),
0,(void*)&out_socketaddr,sizeof(out_socketaddr));

Where MSG is the memory(array) of all those elements and out_socketaddr is the destination

1 Answer 1

2
uint8_t array [sizeof(this) + sizeof(that) + ...];
uint8_t* ptr = array;

memcpy(ptr, &this, sizeof(this));
ptr+=sizeof(this);
memcpy(ptr, &that, sizeof(that));
ptr+=sizeof(that);
...

Avoid making a struct. Although structs will make the code more readable, they also introduce padding, which will be an issue in this case.

Sign up to request clarification or add additional context in comments.

3 Comments

great, i thought it should be something like that, but i tried with woid and it didnt work, ty
will it make difrrence if it will be of type char?
@JohnnyF The raw data should be uint8_t, it doesn't make any sense to represent it as anything else. The char type has implementation-defined signedness, so it shouldn't be used for storing raw data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.