0

i'm currently doing an assignment on making stack in the C language using a header, source, and main file. I'm trying out this code my professor made but there's an error in the header file i couldn't solve. Code: stack.h

#ifndef stack_H
#define stack_H

#include <stdio.h>
#include <stdbool.h>

#define Nil 0
#define MaxEl 10

typedef int infotype;
typedef int address;

typedef struct{
    infotype T[MaxEl+1]; 
    address TOP;         
} Stack;

#define Top(S) (S).TOP //the error is this line
#define InfoTop(S) (S).T[(S).TOP]

/*Body Prototype*/
/*** Constructor***/
void CreateEmpty(Stack *S);

bool isEmpty(Stack S);

#endif // stack_H

stack.c

#ifndef stack.c
#define stack.c

#include "stack.h"
#include <stdio.h>

void CreateEmpty(Stack *S){
    Top(S)=Nil;
}

bool isEmpty(Stack S){
    return Top(S)==Nil;
}
#endif

main.c

#include <stdio.h>
#include <stdlib.h>
#include "stack.h"

int main()
{
    Stack S;
    CreateEmpty(&S);

}

the error says: 'S' is a pointer; did you mean to use '->'?

i'm pretty sure i copied my professor's code almost completely yet the error still persists. Any suggestions on how to fix this problem?

  • 1
    *and main file*. Show that code please. You are likely calling the macros incorrectly and that will be in the main file. – kaylum Apr 08 '21 at 03:50
  • thanks for the rep. i've edited the post to include all 3 files. – Muhammad Riziq Apr 08 '21 at 03:54
  • `Top(S)=Nil;` Needs to be `Top(*S)=Nil;` It may be a bit confusing for you because in some functions `S` is a pointer whereas in others it is a `struct`. Your prof may need some guidance on designing good APIs. – kaylum Apr 08 '21 at 03:54
  • Thanks again for the reply. The code works now. Thank you very much. – Muhammad Riziq Apr 08 '21 at 04:00
  • @MuhammadRiziq If you don't *have* to use those macros, it's more readable to write `S->TOP = Nil;` instead of `Top(*S) = Nil;`. Also, it's rather curious that `isEmpty(Stack S);` would take the argument by value, which forces a *copy* of the struct to be made in order to pass it to the function. – dxiv Apr 08 '21 at 04:01

0 Answers0