Say i have:
unsigned char *varA, *varB, *varC;
varA=malloc(64);
varB=malloc(32);
varC=malloc(32);
How can i put the first 32 byte of varA into varB and the last 32 byte of varA into varC?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s this simple because the underlying data type is
unsigned char, which is the same size as a byte. IfvarA,varB, andvarCwere integers, you would need to multiply the size parameter tomemcpy(i.e. 32) bysizeof(int)to compute the right number of bytes to copy. If I were being pedantic, i could have multiplied 32 bysizeof(unsigned char)in the example above, but it is not necessary becausesizeof(unsigned char)== 1.Note that I don’t need to multiply the 32 in
varA + 32by anything because the compiler does that for me when adding constant offsets to pointers.One more thing: if you want to be fast, it might be sufficient to just work on each half of
varAseparately, rather than allocate two new buffers and copy into them.