Home > Software design >  How to write in a buffer at specific byte in c ?
How to write in a buffer at specific byte in c ?

Time:10-25

I have a char buffer of length 50 bytes. In this buffer, at 20-21 bytes, I want to write a short number, of size 2 bytes, say -1234, specifically at those bytes only? How can I do that?

CodePudding user response:

Looks trivial. Not sure whether this is what you want.

#include <cstring>

char* pc = ...;
short num = ...;

std::memcpy(pc   20, &num, 2);
  •  Tags:  
  • c
  • Related