Thread: Pointers and Arrays

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    2

    Pointers and Arrays

    Hello everyone,

    I'm trying to build a program to evaluate fully parenthesized expressions. We have to use stacks, and we have to use pointers to point to the stacks. I'm having a lot of trouble with the pointers, and I'm debugging it with gdb and a lot of values seem out of whack and some values change while others seem not to, like the *top. here's what I have:
    Code:
    #include <stdio.h>
    #include <stdbool.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    #define STACK_SIZE 20
    
    void push(int* stack, int* top, int element);
    int pop(int* stack, int* top);
    bool is_empty(int* top);
    bool is_full(int* top);
    void stack_overflow(void);
    void stack_underflow(void);
    int evaluate(int i, int j, int k);
    
    int main(void){
        int top1=0,top2=0;
      int *top_operators=&top1;
      int *top_operands=&top2;
      int stack_operands[STACK_SIZE];
      int stack_operators[STACK_SIZE];
      int *operands = &stack_operands[0];
      int *operators = &stack_operators[0];
      char next;
      int temp, temp1, result;
    
      scanf("%c", &next);
      while(next!='='){
        if(next=='+' || next=='-' || next=='*' || next=='/')
          push(operators,top_operators,next);
        else if(isdigit(next)){
          temp=next-'0';
          push(operands,top_operands,next);
        }
        else if(next==')'){
          temp=pop(operands,top_operands);
          temp1=pop(operands,top_operands);
          result=(temp,temp1,pop(operators,top_operators));
          push(operands,top_operands,result);
        }
        else if(next!='('){
          printf("Invalid input.");
          exit(EXIT_FAILURE);
        }
        scanf("%c",&next);
      }
      result=pop(operands,top_operands);
      if(!is_empty(top_operands) || !is_empty(top_operators)){
        printf("Invalid input.");     
        exit(EXIT_FAILURE);
      }
      else{
        printf("%d\n",result);
        return(0);
      }
    }
    
    void push(int* stack, int* top, int element){
      if(is_full(top))
        stack_overflow();
      else
        *(stack+(*top++))=element;
    }
    
    int pop(int* stack, int* top){
      if(is_empty(top))
         stack_underflow();
      else
        return *(stack+(--*top));
    }
    
    bool is_empty(int* top){
      return *top==0;
    }
    
    bool is_full(int* top){
      return *top == STACK_SIZE;
    }
    
    void stack_overflow(void){
      printf("Error. Stack overflow.\n");
      exit(EXIT_FAILURE);
    }
    
    void stack_underflow(void){
      printf("Error. Stack underflow.\n");
      exit(EXIT_FAILURE);
    }
    
     int evaluate(int i, int j, int k){
       int result;
       if(k=='+')
         result=(i+j);
       else if(k=='-')
         result=(i-j);
       else if(k=='*')
         result=(i*j);
       else
         result=(i/j);
       return result;
     }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > *(stack+(*top++))=element;
    > return *(stack+(--*top));
    Whilst the former does what you want - returns *top and then increments what top points to, the same isn't true of the latter.

    --*top just decrements the pointer and then dereferences it for you.
    The result is, you're getting some garbage value.

    It's a very subtle effect of precedence and associativity.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Where do you call evaluate?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    #define STACK_SIZE 20
    
    void push(int* stack, int* top, int element);
    int pop(int* stack, int* top);
    int is_empty(int* top);
    int is_full(int* top);
    void stack_overflow(void);
    void stack_underflow(void);
    int evaluate(int i, int j, int k);
    
    int main(void){
      int top1=0,top2=0;
      int *top_operators=&top1;
      int *top_operands=&top2;
      int stack_operands[STACK_SIZE];
      int stack_operators[STACK_SIZE];
      int *operands = stack_operands;
      int *operators = stack_operators;
      char inp[100],*p=inp,next;
      int temp, result;
    
      scanf("%s",inp);
      while(next=*p++){
        if(next=='+' || next=='-' || next=='*' || next=='/')
          push(operators,top_operators,next);
        else if(isdigit(next)){
          push(operands,top_operands,next-'0');
        }
        else if(next==')'){
          temp=pop(operands,top_operands);
          result=evaluate(temp,pop(operands,top_operands),pop(operators,top_operators));
          push(operands,top_operands,result);
        }
        else if(next!='('){
          printf("Invalid input.");
          exit(EXIT_FAILURE);
        }
      }
      result=pop(operands,top_operands);
      if(!is_empty(top_operands) || !is_empty(top_operators)){
        printf("Invalid input.");
        exit(EXIT_FAILURE);
      }
      else{
        printf("%d\n",result);
        return(0);
      }
    }
    
    void push(int* stack, int* top, int element){
      if(is_full(top))
        stack_overflow();
      stack[(*top)++]=element;
    }
    
    int pop(int* stack, int* top){
      if(is_empty(top))
         stack_underflow();
      return stack[--*top];
    }
    
    int is_empty(int* top){
      return *top==0;
    }
    
    int is_full(int* top){
      return *top == STACK_SIZE;
    }
    
    void stack_overflow(void){
      printf("Error. Stack overflow.\n");
      exit(EXIT_FAILURE);
    }
    
    void stack_underflow(void){
      printf("Error. Stack underflow.\n");
      exit(EXIT_FAILURE);
    }
    
     int evaluate(int i, int j, int k){
       int result;
       if(k=='+')
         result=(i+j);
       else if(k=='-')
         result=(i-j);
       else if(k=='*')
         result=(i*j);
       else
         result=(i/j);
       return result;
     }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with pointers and arrays
    By fusionplus in forum C Programming
    Replies: 5
    Last Post: 09-18-2011, 07:35 PM
  2. Pointers to pointers with arrays
    By Nurumla in forum C Programming
    Replies: 3
    Last Post: 07-18-2011, 11:53 PM
  3. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  4. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  5. Need some help with arrays and pointers
    By NIM in forum C++ Programming
    Replies: 7
    Last Post: 12-12-2003, 01:29 PM