I want to write a a mix of int, char, real in void *data. I am using a file pointer to run through the data block. Now my question is that since the data type is void, I have to typecast it to int while writing integer and char for writing string. While typecasting I used the following sample code:
*((int *)data+0) = 14; //writing int
*((int *)data+4) = 5; //writing int, left a space of 4 bytes for int
*((char *)data+8) = a ; //writing char
*((char *)data+9) = f ; //writing char
但是,在阅读这些价值观时,它会带来正确的价值。
cout<<*((int *)data+0);
cout<<*((int *)data+3);
cout<<*((char *)data+8);
我的法典是否正确? 由于数据无效,我对此表示怀疑。