0

I woud like to ask why i have to use &buffer instead of just buffer, why I am passing adress to reinterpret cast or type conversion. Thanks.

DWORD buffer;
std::ifstream openFile("xxxxx",std::ios::in|std::ios::binary);
std::ofstream writeFile("xxxxx",std::ios::out|std::ios::binary);


while(!openFile.eof())
{
    openFile.read(reinterpret_cast<char*>(&buffer),sizeof(DWORD));
    writeFile.write((char *)&buffer,sizeof(DWORD));
}
user1505497
  • 321
  • 5
  • 13
  • 1
    The `read` and `write` methods require a pointer to the memory to store/retrieve the data, so you must pass the address of `buffer`. – Jonathan Potter Sep 29 '13 at 23:26
  • OT: http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – Barmar Sep 29 '13 at 23:33

1 Answers1

1

Check out this pointers reference. What you have is a variable, prepending a& to tells c++ that you want to refer to the address location of you data, (known as a pointer to the data). This is an effective way to create efficient code as you don't have to copy the data when you pass it to the function, you just tell the function where it is located so it can reference it as needed.

Cheers

CoderDake
  • 1,497
  • 2
  • 15
  • 30