So I've put x into () and gave it the value 1000, well after printing the value I got 0.
1.Why isn't it outputting 1000?
2.Why do I have to give test(i) the value "int i = 0;"? and why can't I use x?
Code:
#include <iostream>
#include <Windows.h>
void test(int x = 1000){
std::cout << "x1: "<< x << std::endl; //x = 0 if declared in parameters
int *y = &x;
*y = x / 2;
std::cout << "x2: " << x << std::endl; //well 0/2 = nothing
printf("\n x = %p; y = %p \n", y, x);
}
int main()
{
int i = 0;
test(i); //why do I have to init. a random value into test() to get it working, why cant I use x?
getchar();
}
But if I do it like that:
Code:
#include <iostream>
#include <Windows.h>
void test2(){
int x = 1000;
std::cout << x << std::endl;
int *y = &x;
*y = x / 2;
std::cout << *y << std::endl;
printf("\n x = %p; y = %p \n", y, x);
}
int main()
{
test2();
getchar();
}
The programm runs perfectly fine, with one exception.
Shouldn't the adress for y be the same as for x, since I'm just overwritting the a value in that specific address?
What am I doing wrong?