more pointer problems

This is a discussion on more pointer problems within the C Programming forums, part of the General Programming Boards category; For some reason the change in pointer location made from a function does not carry back. When I run this ...

  1. #1
    Registered User dharh's Avatar
    Join Date
    Jan 2002
    Posts
    51

    more pointer problems

    For some reason the change in pointer location made from a function does not carry back. When I run this and input something like 1 + 2, I get stack underflow.

    Code:
    #include <stdio.h>    /* supports I/O system */
    #include <stdlib.h>   /* misc declarations */
    #include <string.h>   /* supports string functions */
    
    #define COLS 80
    
    void calculator( char exp[ ] );
    
    void push( char ch, char *stack, char *bos );
    char pop( char *stack, char *tos );
    int gets_s( char *buf, int size );  /* fgets with stdin */
    
    int main( void ) {
      char exp[ COLS ];  /* the expressions */
      
      gets_s( exp, COLS );
      
      calculator( exp );
    
      return 0;
    }
    
    void calculator( char exp[ ] ) {
      static char *stack;    /* pointer to some free stack memory */
      static char *tos;  /* top of stack */
      static char *bos;  /* bottom of stack */
      stack = ( char * ) malloc( COLS * sizeof( char ) );
    
      if ( !stack ) {
        printf( "Allocation Failure" );
        exit( 1 );
      }
      tos = stack;
      bos = stack + ( COLS - 1 );
      
      printf( "before parse: %s\n", exp );
      push( 'a', stack, bos );
    
      pop( stack, tos );
    
    }
    
    void push( char ch, char *stack, char *bos ) {
      if ( stack > bos ) {
        printf( "Stack Full\n" );
      }
      else {
        *stack = ch;
        stack++;
        *stack = '\0';
        //printf( "pushed: %s\n", stack - 1 );
      }
    }
    
    char pop( char *stack, char *tos ) {
      stack--;
      if ( stack < tos ) {
        printf( "Stack Underflow\n" );
        return NULL;
      }
      
      return ( *stack );
    }
    
    int gets_s( char *buf, int size ) {
      int error;  /* returns null on error */
      int c;      /* a single "character" */
      char *ptr;  /* pointer to first \n in string */
    
      if ( fgets( buf, size, stdin ) != NULL ) {
        if ( ( ptr = strchr( buf, '\n' ) ) != NULL ) {
          *ptr = '\0';
          error = 1;
        }
        else {
          while ( ( ( c = getchar( ) ) != '\n' ) && ( c != EOF ) ) { }
          error = NULL;
        }
      }
      else {
        error = NULL;
      }
    
      return ( error );
    }

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    What OS/compiler are you using? When I compile it using MSVC 6 and XP, run it and enter '1 + 2', I get no errors.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User dharh's Avatar
    Join Date
    Jan 2002
    Posts
    51
    Borland 5.5 free command line compiler. I don't get errors on compile, I get run time error, the in program code error "stack underflow".

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    8
    Use ptr-to-ptr

    Code:
    //Function call would be
    push( 'a', &stack, bos );
    
    //function prototype
    void push( char ch, char **stack, char *bos );
    
    //function definition
    void push( char ch, char **stack, char *bos ) {
      if ( (*stack) > bos ) {
        printf( "Stack Full\n" );
      }
      else {
        **stack = ch;
        (*stack)++;
        **stack = '\0';
        //printf( "pushed: %s\n", stack - 1 );
      }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with passing an array of structures by pointer
    By raptor1770 in forum C Programming
    Replies: 9
    Last Post: 11-29-2008, 10:01 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. base class pointer problems
    By ... in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2003, 10:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21