Let’s say I’ve calloc’d some memory for myself:
byte *header = calloc(5 + ZHEADERSIZE, sizeof(byte));
This gives me an array of 0’s. But what if I want to make a custom initialization of the memory? The following code is a fairly detailed initialization for an array literal. Note that it actually contains variables, so I can’t just memcpy all of the array over. I’m wondering if I can replicate this style of initialization for a block of memory that is malloc’d out without having to insert them manually.
unsigned char zhead[] =
{
0x00, 37, 0, 218, 0xFF,
0x50, 0x4b, 0x03, 0x04,
0x14,
0x00,
0x00, 0x00,
0x08, 0x00,
0x08, 0x03,
0x64, 0x3c,
0xAA, 0xBB, 0xCC, 0xDD,
csize, (csize>>8), 0, 0,
uncsize, (uncsize>>8), 0, 0,
0x07, 0x00,
0x00, 0x00,
'r', '/', 'r', '.', 'z', 'i', 'p'
};
You cannot initialize dynamically allocated memory in C other than to zero using
calloc. You can however provide a static prototype array from which you copy the data: