I was looking up the stack, and trying to implement it in an array, and came across this code:

void push(STACK *ps, int x)
        if (ps->size == STACKSIZE) {
            fputs("Error: stack overflow\n", stderr);
        } else
            ps->items[ps->size++] = x;

I know what it all does, except the "->" part. In Wikipedia (where I found the code), it say; "It is responsible for inserting (copying) the value into the ps->items[] array and for incrementing the element counter (ps->size)".

So does the "->" symbol mean "copying to"? Because in the code, it seems like it's taking the size of STACK, and comparing it to STACKSIZE, rather than copying it to it. I realize this is C code, but I was also wondering if such a symbol exists in C++.


Recommended Answers

Do you know what structs (or classes) and pointers are?

Jump to Post

All 5 Replies

Do you know what structs (or classes) and pointers are?

That operator is used for accessing members from a pointer:

myObj *object = new myObj;
//object.myfunc(); error!

for normal objects you can use the dot. But for accessing members from pointer use the ->. I guess this works too:


not sure, though

It's called the dereference operator

Hey -> is not for copying. -> is the membership operator for the structures of pointers means for accessing the items in the STACKS structure you have to use -> operator. This is only for pointers accessing. For normal variables accessing means we have to use . operator

Thanks everyone! Guess it's time to go research pointers more in-depth! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.