JohnDotOwl
JohnDotOwl

Reputation: 3755

Push Pointer of Element into Stack

I'm trying to push a pointer of my element into the stack so it would return a pointer instead of the element.

Based on my limited understanding it's returning the element but not the pointer.

typedef struct Stack
{
    int capacity;
    int size;
    TLDNode* elements;
}Stack;


void push(Stack *S,TLDNode *element)
{

    S->elements = element;
    S->size = S->size + 1;
    return;
}



 Stack *S;
    S = (Stack *)malloc(sizeof(Stack));
    S->elements = ( TLDNode *)malloc(sizeof( TLDNode)*100);
    S->size = 0;
    S->capacity = 100;

    PUSHTOSTACK(tld->head, S);

void PUSHTOSTACK(TLDNode *root,Stack *S) {

    PUSHTOSTACK(S,root);

}

Upvotes: 0

Views: 1109

Answers (1)

DaoWen
DaoWen

Reputation: 33019

Your elements member of your stack struct has type TLDNode*, which is equivalent to an array of TLDNodes. What you want is an array of pointers to TLDNodes, so you need another * in there:

typedef struct Stack
{
    int capacity;
    int size;
    TLDNode** elements;
}Stack;

Technically this is just a pointer to a pointer to a TLDNode, but this is basically equivalent to an array of pointers, as demonstrated by the following code snippet:

TLDNode *node_array[10]; // Array of 10 pointers to TLDNodes
TLDNode **elements = node_array; // node_array is assignable to elements

Upvotes: 1

Related Questions