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);
            abort();
        } 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++.

Thanks

That operator is used for accessing members from a pointer:

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

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

(*object).myfunc();

not sure, though

Edited 5 Years Ago by M3SSIAH: n/a

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! :)

This article has been dead for over six months. Start a new discussion instead.