2

So I'm transitioning from Java to C++ and am still having trouble trying to get my mind wrapped around how pointers work >.< Hope some of you seasoned C++ programmers can help me out!

In class we created both a stack and queue implementation using a dynamic array.

My teacher used pointers to create these arrays on the heap.

int* data = new int[25];

What I don't understand is how can you insert values into the array with "data[top]"? I thought pointers just held the memory address? I would ask my teacher how this works but I'm on a time crunch and she won't be able to get back to me till tomorrow afternoon >.<

Stack::push(int value) {
    if(top==size) {
        resize();
    }
    data[top] = value;
    top++;
}

3 Answers 3

2

I thought pointers just held the memory address?

Yes, but you're allowed to do things with that memory address. In particular C++ allows something called 'pointer arithmetic' which allows you to use a memory address to obtain the address of other memory located relative to the memory you already have the address for. E.g., if you have a memory address you can get the address of the memory located immediately after it.

(the squares are memory locations)

☐
☐ 
☐  ← I have the address of this memory in variable A
☐  ← I would like to get the address of this memory location and to store it in X
☐
☐
☐


int *A = ...;
int *X = A + 1; // compute the address of the next memory location

So an array is a series of memory locations. To access any element of the array you simply take the address you have, compute the address of the element you want to access, and then use that new address.

int *A = new int[10];
int *X = A + 5; // get the address of the memory five locations past where A points to
*X = 999;

You don't have to store the address you compute in a variable:

int *A = new int[10];
*(A+5) = 999;

C++ provides a shorthand for the syntax *(A+5), this is the array index operator:

int *A = new int[10];
A[5] = 999;

One thing that's interesting is that the array index operator really is just shorthand for this *(A+5) expression. Since you can flip around the operands and do *(5+A) you can do the same with the array index operator:

5[A] = 999;

You shouldn't do that though, because it's not very readable.


Another thing to know about pointers: Java has pointers. When you do

String s = new String();

in Java s is a pointer. Java just tries to hide this fact at the same time that it requires the use of pointers to a much greater extent than C++ does. Java doesn't have pointer arithmetic, and you don't have to manually dereference pointers in Java like you have to in C++. But consider:

List<String> l = new List<String>();
List<String> m = l; // Do I have two lists, l and m, that can be modified independently? Or do I have two entities, l and m, that both refer to the same List object?

And remember those Null Pointer exceptions you get in Java.

If you've been using Java then you've already been using pointers. They're not really all that different in C++, but they're directly visible and explicit in C++ instead of being poorly hidden the way Java has them.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for taking the time to write such a thorough explanation! I really appreciate your help, thank you!!
@edA-qamort-ora-y you should try it out or read the standard. 5[A] works exactly as I've described. ideone.com/yw6UJ
I stand corrected. The Note at 8.3.4-6 explitly states it is commutative. Weird.
0

One way to look at it is data[top] is the same as *(data + top). So you take the pointer data, add the value top to it (multiplied by the size of an int), then read or write that location.

1 Comment

ahhhhh I get it now thanks alot for the quick reply!! *(data + top) is a great way to put it!
-1

See: C++ Pointers & Arrays

And you are correct, pointers only hold addresses to points in memory where the data is actually found. The stack you are creating is just a class that provides a specific way of getting memory from and adding memory to the array.

Also check out: C++ STL Stack

That should clarify how stacks work.

1 Comment

To whoever downvoted this, I am fine with that. But next time it would be more constructive to tell me why this answer is not helpful. Not only so I can see, but so others can see why this answer is wrong/not-useful, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.