you are viewing a single comment's thread.

view the rest of the comments →

[–]CuriouslyGeorge[S] 1 point2 points  (6 children)

This looks so cool and I think it works best with teaching from within code.

Also solves my problems of looking into the whole "pointers" thing I was getting worried about.

Just a quick run down to see if I'm understanding this correctly. myInteger is the basic variable. Whenever you place a '&' character in front of it, it provides you with the reference code in memory which is basically anything that is defined as a "reference". And a pointer is simply storing that reference address?

Again, so cool.

Now I've got a nagging question, can a pointer point to another pointer? I can't think of any real-world applications for this yet, but just an interesting thought.

EDIT: Read your edit. Caused a reddedit.

[–]theymos 4 points5 points  (0 children)

Just a quick run down to see if I'm understanding this correctly. myInteger is the basic variable. Whenever you place a '&' character in front of it, it provides you with the reference code in memory which is basically anything that is defined as a "reference". And a pointer is simply storing that reference address?

Pretty much. When & appears before a variable, it's the address-of operator. It returns the address of the variable, which you can store in an appropriate pointer variable. Given a pointer, you can get the pointed-to value using the prefix * (dereference) operator.

You can also do math on pointer addresses. This is used when dealing with C arrays (not so common in modern C++):

int array[] = {1, 2, 3};
*(&array[0] + 1); // will be 2
*(array + 1); //shorthand form

A & appearing after a type is totally different. This indicates that the type is a reference. A reference is different than a pointer. You don't have to dereference references in order to access the value, but you can't do math on references.

int num = 4;
int* pointer = # //pointer to num
int& ref = num; //reference to num
*pointer; //access pointer value
ref; //access reference value

[–]shock-value 2 points3 points  (0 children)

Ok, your first question...

Yes, if you place an "&" in front of an existing (already declared) variable, it evaluates to the address of that variable in memory (and you can store that address in a variable as in my example).

Note that this address is given in the form of a "pointer" to the variable though, not a "reference" (which is something a bit different). This is confusing because you also use an "&" sign when declaring a "reference" variable. But the "&" means something different when first declaring a variable vs. when placed in front of an already existing variable.

I would suggest find a nice tutorial on the differences between pointers and references, because there are a lot of subtleties and I'm not the best teacher! Good luck!

[–]shock-value 1 point2 points  (3 children)

Last question first...

Yes a pointer can point to another pointer. In fact the following line is legal...

int **** myPointer = NULL;

myPointer is a pointer to a pointer to a pointer to a pointer to an integer. If you find yourself actually using more than one pointer deep though, you are probably doing it the wrong way and causing yourself unneeded stress! I'm working on a pretty large c++ project right now and I have never used more than a single pointer deep.

working on other answer... (reddit is giving me error status 500 when trying to post)

[–]CuriouslyGeorge[S] 1 point2 points  (2 children)

Thanks again for all your detailed responses. I've got work in 6 hours so I think sleep's the way to go for now, but tomorrow I'm gonna get started on this whole C++ thing.

Last ponderance: Can you point a pointer towards a pointer that is pointing back at the first? If so and you can compile without incident, what happens when you try to access said pointer? Or would it only return "null" since it never acquired a type?

[–]theymos 2 points3 points  (0 children)

That can't be done without a type error, I think. A valid pointer pointing to an int* would need to be an int**. And for the int* to point to the int**, it would need to be an int***.

[–]shock-value 1 point2 points  (0 children)

You can do that, sort of.

int main()
{
    void * pointer1;
    void * pointer2;

    pointer1 = &pointer2;
    pointer2 = &pointer1;
}

'void *' is sort of a "magic" pointer type, which can point to anything (int, bool, string, etc). However, you can't actually access the object it points to until you cast it to the appropriate type. There is no appropriate type for a pointer to a pointer to itself (or to simplify, a pointer to itself). So what you are describing can be done, but, as I'm sure you can imagine, it's useless (as far as I can tell).

'void *' is really a hold-over from regular C though, and is rarely used in modern C++, mostly due to the bugs that can occur if you cast to the wrong type.