Roman Roman - 1 year ago 86
C++ Question

How does pointer in function work at this code?

int f_point(int kek,int *lol) {
*lol *= *lol;
return kek;

int main {
int x;
std::cin >> x;
int *adress_of_x = &x;
int a,b = f_point(x,&x); //how does it work?
std::cout << a << LINE_JUMP;
std::cout << b << LINE_JUMP;

For example, if I give 2 to program then I will get 0 and 2. Why?

Answer Source
 int a,b = f_point(x,&x); //how does it work?

The variable declaration leaves a uninitialized and initializes b from the result of f_point(x,&x);.

Since it's an uninitialized variable, accessing the value of a in the

 std::cout << a << LINE_JUMP;

statement leads to undefined behavior of your program. Having an output of 0 is just one of any possibilities (including your fridge explodes unexpectedly or little demons flying out of your nostrils).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download