confused with this

Pages: 12
Line 17: You're taking the address of x (a pointer) and passing it to functionA() which is expecting a pointer.

Line 8: You're dereferencing x. i.e. changing what x points to. You're not changing x.


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
Last edited on
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.
Last edited on
Does that help you? :)
Does that help you? :)


Does that help you increase your post count? :+)
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.



but I thought I was changing what X points to
Last edited on
ok take for example


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29





#include <iostream>

using namespace 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?
Last edited on
and another example why does this code change the contents of x yet the code above does not change the contents of number.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24


#include <iostream>

using namespace std;

int functionA(int *x){

     *x = 70;
     return *x;

}

int main()
{
   int x = 60;
   cout << x << endl;
   functionA(&x);
   cout << x << endl;

}


Here is a version which does what you want :+)

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
void functionA(int *x) {
  int y = 55;
  *x = y;
}

int main() {
   int number = 60;
   std::cout << &number << " " <<  number << "\n";
   functionA(&number);
    std::cout << &number << " " <<  number << "\n";
}


0x79ee8b28df5c 60
0x79ee8b28df5c 55


One thing with your code, your function was declared to return a pointer to int, but nothing was done with that value in main.

If you do this:

cout << functionA(&number);

It prints 55 :+)
Last edited on
thanks TheIdeasman yeah I was messing around and got that result I know how passing by ref and value works well I thought I did haha

but I'm still confused as to why this line of code does not change the value in number

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

#include <iostream>

using namespace 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21


#include <iostream>

using namespace 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
closed account (48T7M4Gy)
@cire says:
.
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.
I think you got the wrong thread Kermort =)

do you have any idea why the value of number doesn't change I've watched about 15 youtube videos and not one actually explains this problem =(
Topic archived. No new replies allowed.
Pages: 12