#include <iostream> int ee = 7; void qwerec( const void* buf ) { buf = (char * )& ee; } int Rec() { int qr = 0; qwerec( (char * )&qr ); return qr; } int main(int argc, char* argv[]) { int qq = 5; qq = Rec(); std::cout << qq; system ( "pause" ); return 0; } 

Smart people, tell me why prints 0, not 7?

    1 answer 1

    Try to write like this:

     void qwerec( const void* buf ) { *((int*) buf) = ee; } 
    • no :) the question is why the code (the one that is written) - displays 0 how to do otherwise, then I know :) no ideas? either I just sat out and my head didn’t see the obvious things, or something was wrong ... - Stasik Kovalenko
    • It's simple. Think about how parameters are passed to a function in C / C ++. A copy of the passed parameter is created inside the function, and when a value is assigned to it, it is not passed outside. In order to change a variable outside a function, it must pass a pointer to it and, when assigned, dereference it, i.e. Refer to the memory area to which it points. This is done using the operator * (pointer). - skegg
    • mda .. that I was stupid - I took the address, passed it as a parameter, changed it and waited for something to happen) thanks :) - Stasik Kovalenko