Can anybody explain me this 'kind of casting'¿?
I have found this code to write data:
inv is an 'inventory' array.
I dont undertad he line that writes data, it makes some casting but ....
Can anybody help me ? ( I dont understand (const char*) neither & for inv.
thanks
Code:
struct inventory {
char item[20];
int quantity;
double cost;
};
for(int i=0; i<3; i++)
fout.write((const char *) &inv[i], sizeof(inventory));
Re: Can anybody explain me this 'kind of casting'¿?
It all about basic C++. Read about explicit casting and pointers/references.
Re: Can anybody explain me this 'kind of casting'¿?
Re: Can anybody explain me this 'kind of casting'¿?
Isn't that pretty bad code since struct padding might change the binary file between systems? I think you should not use that code.
Re: Can anybody explain me this 'kind of casting'¿?
Its true that struct padding might change the structure size, but if your only interested in one system (or the file is only written/read on one system, eg. an internal data file) then its fine. It just means more work later on if you want the make the files portable between different architectures/systems.
Re: Can anybody explain me this 'kind of casting'¿?
And possibly some garbage (possibly containing some sensitive data) written to the file.
Re: Can anybody explain me this 'kind of casting'¿?
Always pack your structs if they are going to leave the program...