pointers

Dec 22, 2013 at 4:37pm
what does x stand for here ? does it matter if I did it this way , or declared then used = ?

 
  int *ptr,x;
Dec 22, 2013 at 4:38pm
x is an int

1
2
3
4
5
6
// this:
int *ptr,x;

// is the same as this:
int* ptr;
int x;
Dec 22, 2013 at 4:48pm
Thank you , can I ask you something , when I try to show the location in the memory for pointers I'll use this cout<<&ptr;
but if I used this cout<<*&ptr;

it shows another location , what's the difference ?
Dec 22, 2013 at 4:55pm
A pointer is a variable which contains the address of another variable.

cout << &ptr; will print the address of the 'ptr' variable.

cout << *&ptr; is the same as cout << ptr; which prints the contents of 'ptr'... ie, it prints whatever address is stored inside the ptr variable.

Example:

1
2
3
4
5
6
7
8
int x = 5;
int* ptr = &x;  // ptr points to x

cout << &x; // prints the address of x
cout << ptr; // prints the contents of ptr -- which prints the address of x (same as above line)
cout << *&ptr;  // same as above lines
cout << &ptr;  // prints the address of 'ptr', which will be different from address of x
   // since they are 2 separate vars. 
Dec 22, 2013 at 5:23pm
thank u
Dec 22, 2013 at 5:25pm
closed account (EwCjE3v7)
Pointers are difficult at start but once you get the hang of it .. They become really easy to use
Dec 22, 2013 at 6:11pm
Yeah , I know :(

can I ask you another question , I don't want to open a new thread so I'll post here ^^

after assigning a pointer , can I pass it to a function ? by value or by ref ? or it's constant that I cannot modify ?

Last edited on Dec 22, 2013 at 6:13pm
Dec 22, 2013 at 6:31pm
ah , I just tried it myself , and I got the idea
Dec 22, 2013 at 6:57pm
Passing something by reference can be seen as passing the address of something.

Passing a pointer by reference would pass the address of the pointer storing the address of an object. So don't do it unless the function is there to change what the pointer is pointing.
Dec 22, 2013 at 7:41pm
closed account (EwCjE3v7)
There you go.

Please don't double post. You can edit your old post and write
"Edit:...." . But it's fine. No hate. It's fine just putting that out there.
Dec 22, 2013 at 9:51pm
What Cfz did isn't that bad.

Double posting is obnoxious when it turns into spam, or is being used to artificially inflate one's post count.

I don't think that's the case here. Though the advice stands.
Topic archived. No new replies allowed.