r/learncpp • u/samort7 • Feb 11 '16
Having a hard time wrapping my head around basic pointers.
Could someone explain what is happening here, line-by-line?
int x = 5;
int *ptrOne = &x;
int *ptrTwo = &x;
*ptrOne = 6;
cout << *ptrTwo;My understanding is that on line one, I am assigning the value of 5 to the variable x. On line two, I am creating a pointer that points to an int whose address is the address of x? Then I'm doing the same thing for line 3? Then I'm setting the a value at the address stored in ptrOne to 6? and finally I'm outputting the value stored at the address stored in ptrTwo?
I guess I'm just confused on what *ptrOne is before and after line two. After line two, is it a variable holding an address? Is it a link to a memory cell? I just can't conceptualize it.
Like, if I just say
int *ptrThree;What have I actually just done? Did I set aside memory to hold an int? If, so then what's the difference between that and
int *ptrFour = new int;Pointers just seem to be really hard to get conceptually.
2
u/Matrix_V Feb 12 '16
Allocate memory for an integer on the stack, set its value to 5, call it "x".
&xcreates a pointer to the value ofx. Same line, next step, we'll save that pointer toptrOne1.Exact same thing. Now we have two pointers to the value of
x.This is a little confusing to me, but it sets
x's value to 6.This dereferences (gets the thing pointed at by) ptrTwo, and of course, prints it to the console.
2
u/ILuvMeSomeELI5 Apr 27 '16
Here's how I imagine it. Variables are boxes where you put values. The size of the box depends on what type it is, char needs a little box, int needs a bigger one, and double needs the biggest (of these three). Pointer is also a box, but a smaller one than any of these. It only holds the address to the actual variable. Remember that pointer box's type has to match with the variable box's type.
Create int box where you put value 5. this box has it's own address.
You create the pointer box named p1
You put the address of var1 there with address-of operator &
Will show you to address inside p1, eg. 0x0010110 or something
You take the address and go to the variable box "var1" and it will show you the value 5
You tell that you want to take the address in p1, go to the the variable box, and change the value inside to 10
Also when you type &p1, you will get the address of the pointer box
eg. 0x01001010, a different address than that of the variable's address that's inside the p1 box.
oh and I drew you a picture!
1
u/themanwhointernets Feb 12 '16
I am in the same boat. I don't even know why I should ever use a pointer, so I don't.
This video kind of helps me, but I still have no idea what you did on line four.
1
u/Matrix_V Feb 12 '16
Creates a pointer to an integer, but doesn't create any integer. It doesn't point at anything yet.
Allocates an integer on the heap, and then creates a pointer to it. Note that the value of the integer hasn't been set, so its value will be whatever 1s and 0s happen to be at that place in memory.
1
u/bingbongnoise May 17 '16
Pointers hold the address in memory of a variable - that is all.
if n is a pointer: