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

Recommended Answers

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();
//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

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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.