-5

For My class , I am supposed to translate a code that was provided to us and translate it from Java to C. This is my first encounter with C. So i completed that . After Compiling i receive about 6 errors saying that Stack is not declared.And Stack is used across the code. Can someone help point out where I went wrong.

// $Id: crpn.c,v 1.28 2014-04-08 15:23:19-07 - - $
// NAME
#include <assert.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>

int exit_status = EXIT_SUCCESS;
#define EMPTY (-1)
#define SIZE 16

struct stack{
   int top;
   double numbers[SIZE];
};

void bad_operator (const char *oper) {
   fflush (NULL);
   fprintf (stderr, "oper=\"%s\"\n", oper);
   fflush (NULL);
   exit_status = EXIT_FAILURE;
}

void push (struct stack *the_stack, double number) {
   if(stack->top >= SIZE - 1){
      printf ("%s: stack overflown%n", number);
   }else{
       stack->numbers[++stack->top] = number;
   }
}

void do_binop (struct stack *the_stack, char oper) {
   if (stack->top < 1) {
      printf ("'%s': stack undeflow", oper);
   }else{
      double right = stack->numbers[stack->top--];
        double left = stack->numbers[stack->top--];
      switch (oper) {
         case '+': push (stack, left + right); break;
         case '-': push (stack, left - right); break;
         case '*': push (stack, left * right); break;
         case '/': push (stack, left / right); break;
      }
   }
}

void do_print (struct stack *the_stack) {
   if (stack->top == EMPTY) {
      printf ("stack is empty%n");
   }else {
      for (int pos = 0; pos <= stack->top; ++pos) {
          printf ("%s%n", stack->numbers[pos]);
      }
   }
}

void do_clear (struct stack *the_stack) {
   stack->top = EMPTY;
}

void do_operator (struct stack *the_stack, const char *oper) {
   switch (oper->charAt(0)) {
      case '+': do_binop (stack, '+'); break;
      case '-': do_binop (stack, '-'); break;
      case '*': do_binop (stack, '*'); break;
      case '/': do_binop (stack, '/'); break;
      case ';': do_binop (stack);      break;
      case '@': do_clear (stack);      break;
      default : bad_operator (oper);   break;
   }
}
int main (int argc, char **argv) {
   if (argc != 1) {
      fprintf (stderr, "Usage: %s\n", basename (argv[0]));
      fflush (NULL);
      exit (EXIT_FAILURE);
   }
   struct stack the_stack;
   the_stack.top = EMPTY;
   char buffer[1024];
   for (;;) {
      int scanrc = scanf ("%1023s", buffer);
      if (scanrc == EOF) break;
      assert (scanrc == 1);
      if (buffer[0] == '#') {
         scanrc = scanf ("%1023[^\n]", buffer);
         continue;
      }
      char *endptr;
      double number = strtod (buffer, &endptr);
      if (*endptr == '\0') {
         push (&the_stack, number);
      }else if (buffer[1] != '\0') {
         bad_operator (buffer);
      }else {
         do_operator (&the_stack, buffer);
      }
   }
   return exit_status;
}

Error Message:

crpn.c: In function ‘push’:
crpn.c:25:7: error: ‘stack’ undeclared (first use in this function)
    if(stack->top >= SIZE - 1){
       ^
crpn.c:25:7: note: each undeclared identifier is reported only once for each function it appears in
crpn.c:26:7: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘double’ [-Wformat=]
       printf ("%s: stack overflown%n", number);
       ^
crpn.c:26:7: warning: format ‘%n’ expects a matching ‘int *’ argument [-Wformat=]
crpn.c:24:26: warning: unused parameter ‘the_stack’ [-Wunused-parameter]
 void push (struct stack *the_stack, double number) {
                          ^
crpn.c: In function ‘do_binop’:
crpn.c:33:8: error: ‘stack’ undeclared (first use in this function)
    if (stack->top < 1) {
        ^
crpn.c:34:7: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
       printf ("'%s': stack undeflow", oper);
       ^
crpn.c:32:30: warning: unused parameter ‘the_stack’ [-Wunused-parameter]
 void do_binop (struct stack *the_stack, char oper) {
                              ^
crpn.c: In function ‘do_print’:
crpn.c:48:8: error: ‘stack’ undeclared (first use in this function)
    if (stack->top == EMPTY) {
        ^
crpn.c:49:7: warning: format ‘%n’ expects a matching ‘int *’ argument [-Wformat=]
       printf ("stack is empty%n");
       ^
crpn.c:47:30: warning: unused parameter ‘the_stack’ [-Wunused-parameter]
 void do_print (struct stack *the_stack) {
                              ^
crpn.c: In function ‘do_clear’:
crpn.c:58:4: error: ‘stack’ undeclared (first use in this function)
    stack->top = EMPTY;
    ^
crpn.c:57:30: warning: unused parameter ‘the_stack’ [-Wunused-parameter]
 void do_clear (struct stack *the_stack) {
                              ^
crpn.c: In function ‘do_operator’:
crpn.c:62:16: error: request for member ‘charAt’ in something not a structure or union
    switch (oper->charAt(0)) {
                ^
crpn.c:63:27: error: ‘stack’ undeclared (first use in this function)
       case '+': do_binop (stack, '+'); break;
                           ^
crpn.c:67:7: error: too few arguments to function ‘do_binop’
       case ';': do_binop (stack);      break;
       ^
crpn.c:32:6: note: declared here
 void do_binop (struct stack *the_stack, char oper) {
      ^
crpn.c:61:33: warning: unused parameter ‘the_stack’ [-Wunused-parameter]
 void do_operator (struct stack *the_stack, const char *oper) {
DJ023
  • 1
  • 3

2 Answers2

1

'stack' Undeclared Error

stack is the type but your pointer is the_stack

void push (struct stack *the_stack, double number) 

so you need to use the_stack instead of stack in such statements

   if(stack->top >= SIZE - 1){
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
1

The variable is declared as the_stack:

void push (struct stack *the_stack, double number) {

But you refer to it as stack:

 if(stack->top >= SIZE - 1){

There is no variable stack in your code, so the compiler complains about unknown identifiers. struct stack is the base type of the variable the_stack (the full type is struct stack *).

ApproachingDarknessFish
  • 14,133
  • 7
  • 40
  • 79