I am creating a program that uses a basic stack in C. In this I have two structures defined in the heading:
- A structure named Node with a string and a pointer to a previous Node as members.
A structure named Stack with a pointer to the last Node as member.
Here are the definitions of these structures in my header file:
#include <stdio.h> #include <stdlib.h> typedef struct Node { const char* string; struct Node *prev; }; typedef struct Stack { size_t sizeOfStack; size_t sizeOfElem; struct Node *last; };
One method giving me errors is CreateStack():
CreateStack: This function creates a stack (equivalent to a constructor).
(a) Name: CreateStack
(b) Return Type: A pointer to a stack allocated in the heap.
Here is my implementation
Stack* CreateStack() {
Stack* stack = malloc(sizeof(*stack));
if (stack == NULL) {
return NULL;
}//end of if
stack->sizeOfElem = 0;
stack->sizeOfStack = 0;
stack->last = NULL;
return stack;
}//end of CreateStack
But the compiler is spitting this out:
error: 'Stack {aka struct Stack}' has no member named 'last' stack->last = node;
error: 'Stack {aka struct Stack}' has no member named 'last' node->prev = stack->last;
error: 'Stack {aka struct Stack}' has no member named 'last' Node *node = stack->last;
If someone could point out the issue here I would greatly appreciate it. I am confused as to why it is saying last is not a thing, yet prev defined in the same way in the other structure does not raise a flag. Thanks.