oh man at this stage I think I'm going to pull my hair out haha,I'm just soooo lost
Line 16: The compiler allocates space for a pointer (num). Let's call this stack[1]. You call returnPointer passing a copy of the address of stack[0]. This copy is at stack[2].
I just don't get how I'm only passing a copy of number( stack[0]) into the arguements I thought when you pass an address into something you are passing by reference always :s
Line 8: You modify the argument (passed pointer x) which is at stack[2] with the address of y, which is stack[3]. You're NOT changing what x points to nor are you changing the pointer in the caller.
but I thought I was changing what X points to I always thought when you do the operation
1 2
int x = 3;
int *pointer = &x;
you are pointing to where you assign it(pointer points to the memory address x)
I've been reading through this for hours and I just can't figure this out =( those are the only two parts that are still driving me crazy
You seem to take too long to understand something I think...
1 2 3 4
int x = 3;
int *pointer;
pointer = x;
cout << *pointer;
.
1 2 3 4
int x = 3;
int *pointer;
pointer = (int*)x;
cout << *pointer;
So how things will go without the address operator (&). Assuming the compiler doesn't prevent it, you make the pointer store the actual value stored in the variable (x) as the memory address. Since (int) variable type is for general calculation purpose the value of an (int) can never be a valid memory address. Now (pointer) points to the memory address of 0x3, if you attempt to dereference it your program will encounter "Access Violation".
1 2 3 4
int x = 3;
int *pointer;
pointer = &x;
cout << *pointer;
That's why the memory address operator (&) exists. It is made exclusively just for giving correct and valid values to pointers. Pointers just love memory addresses. By using it, you force the variable to give its memory address instead of the actual value stored in the variable.
thanks for the reply Closed account I already understand how pointers work I just understand why my code is behaving in a way I didn't expect if you look at anons example on page one these are the two lines I just can't figure out
Line 16: The compiler allocates space for a pointer (num). Let's call this stack[1]. You call returnPointer passing a copy of the address of stack[0]. This copy is at stack[2].
I just don't get how I'm only passing a copy of number( stack[0]) into the arguements I thought when you pass an address into something you are passing by reference always :s
and
Line 8: You modify the argument (passed pointer x) which is at stack[2] with the address of y, which is stack[3]. You're NOT changing what x points to nor are you changing the pointer in the caller.
#include <iostream>
usingnamespace std;
int functionA(int *x){
int y = 55;
x = &y;
return *x;
}
int main()
{
int number = 60;
cout << &number << number << endl;
functionA(&number);
cout << number;
}
I just don't understand why this doesn't change the value of number,since I was passing in by reference(the memory address) not by copy I thought it would change the value of number?
#include <iostream>
usingnamespace std;
int functionA(int *x){
int y = 55;
x = &y;
return *x;
}
int main()
{
int number = 60;
cout << &number << number << endl;
functionA(&number);
cout << number;
}
and why this code does actually DOES change the value of x
#include <iostream>
usingnamespace std;
int functionA(int *x){
*x = 70;
return *x;
}
int main()
{
int x = 60;
cout << x << endl;
functionA(&x);
cout << x << endl;
so for the first example,I'm passing by reference(memory location) of the integer variable number and it gets passed into the function functionA,now line 14 I thought I'm changing where x(address of number) is pointing to by assigning x to the address of y(&y) now by doing this I thought I would change the value of number to y which is 55 but yet this does not happen,I don't understand why the value does not change here
But, hey.. keep talking out of your ass and filling the forums with misinformation and novice code while pretending you're an expert. I'm sure you're impressing yourself.
This sort of disgraceful language, exaggeration and bullying will not be tolerated Cire. Stop it and apologise.