Reputation: 3755
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
Reputation: 33019
Your elements
member of your stack struct has type TLDNode*
, which is equivalent to an array of TLDNode
s. What you want is an array of pointers to TLDNode
s, 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