QuestionSolvedPointer & putting values into the parameters

Posts 1–4 of 4 · Page 1 of 1
Pointer & putting values into the parameters
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?
If you do something like void test(int x = 1000), you give the parameter a default value, which it will get initialized to if you do not pass it.

This means, that you could have something like this:

int add( int x, int y = 1000 )

If you call add with two arguments ( add( number1, number2 ) ), it will use the two arguments passed in within the function, but if you only pass one argument, it will default initialize the second argument to 1000. add( number1 ) is perfectly valid here, it will use y initialized as 1000.

Since you pass a number to the function, it will use your number instead of the default initialization.

//why do I have to init. a random value into test() to get it working, why cant I use x?
C++ does not guarantee you default initialized variables, which means that if you do not give a variable a value, it will be "initialized" to whatever is at the stack when creating the variable, though i think most compilers default initialize them for you under the hood, but keep in mind that it's not a C++ standard and that C++ alone does not guarantee you default initialized variables.

Shouldn't the adress for y be the same as for x, since I'm just overwritting the a value in that specific address?
y holds an address as value, which is the address of x. A variable has an address, a value and a type. Your pointer is of type int pointer and has some address windows gives your variable at that point in time. The value of your pointer is the address ( the memory address windows gives your variable ) of the variable x. The address of y is different from the address of x, though the value of y is equal to the address of x.
Thank you,
now I get how these argument calls work in there + the overwritting !
Posts 1–4 of 4 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us