Basically what I am trying to do is modify the contents of the place where the structures data pointer points to. I have to add 5 arb characters onto the packet's data.
When I run the program below, it crashes when performing the memcpy and I have no idea why. Please could somone assist me.
Thanks!
Code:struct packet {
int length; // length of the data packet
unsigned char *data; // pointer to data area
};
void addHeader(packet);
void stripHeader(packet);
int main() {
unsigned char *d = (unsigned char*) "45100035A0ED400";
packet p = {15, d};
cout << "Original data: " << d << endl;
addHeader(p);
cout << "headerAdd: " << p.data << endl;
//stripHeader(p);
//cout << "stripHeader: " << p.data << endl;
cin.get(); // pauses program;
return 0;
}
void addHeader(packet x) {
unsigned char temp[20] = {'A','F','A','F','1'};
int count = 0;
for(int i = 5; i < (sizeof(temp)/sizeof(char)); i++) { // appends x.data onto temp
temp[i] = x.data[count];
count++;
}
memcpy(x.data, temp, 20);
}
