1

I am creating a program that uses a basic stack in C. In this I have two structures defined in the heading:

  1. A structure named Node with a string and a pointer to a previous Node as members.
  2. 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.

Barmar
  • 741,623
  • 53
  • 500
  • 612
Peter M
  • 15
  • 5
  • Your compiler errors are not in the code you show. Your typedefs are incomplete. – aschepler Apr 14 '17 at 22:37
  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [**How to create a Minimal, Complete, and Verifiable example**](http://stackoverflow.com/help/mcve)." – David C. Rankin Apr 14 '17 at 22:39
  • @aschepler yes it is lengthy code, so since this error does not seem specific to a method that uses the structures, I presume the error is in the structure. I provided the structures themselves.. All errors reference "last" as non-existent. – Peter M Apr 14 '17 at 22:46

2 Answers2

1

Your typedef statement is incomplete, as you do not define a name for the type. Write the following.

typedef struct Stack {
    size_t sizeOfStack;
    size_t sizeOfElem;
    struct Node *last;
}Stack;

Note the Stack at the end, which defines now type Stack being equivalent to struct Stack.

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
1

Fix the typedefs and it'll compile:

#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
 const char* string;
 struct Node *prev;
} Node ;

typedef struct Stack {
 size_t sizeOfStack;
 size_t sizeOfElem;
 struct Node *last;
} Stack;

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
Petr Skocik
  • 58,047
  • 6
  • 95
  • 142