0

So i keep getting the message invalid expression "..." Bus Error (core dumped)if i type ./rpcalc "..." also if i just type in the command line ./rpcalc 1 i get the Segmentation Fault(core dumped) message. This is my entire code i would appreciate any help.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define max 10
#define NUMBER 'n'



int main(int argc, char *argv[])
{
  double tmp;
   char c;

  if(argc <= 1) {
    fprintf(stderr,"invalid call\n");
    return 1;
  }

    while(--argc)
       {
         c = getop(*++argv);
        switch(c)
          {
         case NUMBER:
          push(atof(*argv));
          break;
         case '+':
          push(pop() + pop());
          break;
         case '-':
          tmp = pop();
          push(pop() - tmp);
          break;
         case '*':
          push(pop() * pop());
          break;
         case '/':
          tmp = pop();

          if(!tmp){
          printf("can't divide by 0\n");
          }
          else{
          push(pop()/tmp);
          }
          break;
         default:
           printf("invalid expression %s\n", *argv);
         }
      }
    printf("%g\n",pop());
    return 0;
}


int push (int stack[max], int *top, int *data)
{
 if(*top == max -1)
    return(-1);
  else
    {
      *top = *top +1;
      stack[*top] = *data;
      return(1);
    }
}

int pop(int stack[max], int *top, int *data)
{
  if(*top == -1)
    return(-1);
  else
    {
      *data = stack[*top];
      *top = *top - 1;
      return(1);
    }
}

 static int isNumber(const char *s){
  if(*s == '-' || *s == '+') s++;
  if(*s == '\0'){
    return 0;
  }

  while (isdigit(*s)) s++;

  if(*s == 'e' || *s == 'E'){
    s++;
    while(isdigit(*s)) s++;
  }
  return *s == '\0';
}

int getop(const char *op){
  return isNumber(op) ? NUMBER : *op;
}
  • 1
    Compile your source code again. But this time, turn on error reporting (try adding `-Wall` to the command line). You will see a long list of errors in your source code. Fix those first. – r3mainer Mar 09 '15 at 01:55

1 Answers1

0

gcc -Wall -ansi -pedantic

First, you probably shouldn't be returning a static int. Return either a char or an int or unsigned int.

Next: You're invoking functions with incorrect parameters:

Line 28:

push(atof(*argv));

This is not how you've defined your function.

int push (int stack[max], int *top, int *data);

It requires an array of stack[max], an int pointer, and another int pointer

Passing in a float is not correct.

Actually it looks almost like all of your function calls are with incorrect parameters.

jake
  • 65
  • 1
  • 3