Making a Stack using Pointers

This is a discussion on Making a Stack using Pointers within the C Programming forums, part of the General Programming Boards category; Hi remember me? I posted here a few months ago with a problem using a stack. Well now I知 using ...

  1. #1
    Unregistered
    Guest

    Question Making a Stack using Pointers

    Hi remember me? I posted here a few months ago with a problem using a stack. Well now I知 using the same stack code again but trying to make the stack with pointers (yay I知 learning pointers!). Anyway I have been playing with this code for a long time and I am nearly insane because it is not working and I cant find out where the problem is. I know it is probably a stupid and small mistake of mine but I cant find it.
    When this program is run if the input is 123 the output should be 321
    I知 sure that this code is very easy to understand for anyone who knows C better than me so please tell me what is wrong with my code before I go crazy.
    Thankyou.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define STACK_SIZE 50
    #define TRUE 1
    #define FALSE 0
    
    typedef int Bool;
    
    void make_empty(void);
    Bool is_empty(void);
    Bool is_full(void);
    void push(int i);
    int pop(void);
    void stack_overflow(void);
    void stack_underflow(void);
    
    int contents[STACK_SIZE];
    int *top_ptr = &contents[0];
    
    int main()
    {
      int temp = 0;
    
      make_empty();
      printf("Enter some digits: ");
      while ((temp = getchar()) != '\n') {
        push(temp);
      }
    
      printf("Contents of stack (should be reversed): ");
      while (!is_empty()) {
        printf("%d", pop());
      }
      printf("\n");
    
      return 0;
    }
    
    void make_empty(void)
    {
        top_ptr = &contents[0];
    }
    
    Bool is_empty(void)
    {
        return top_ptr == &contents[0];
    }
    
    Bool is_full(void)
    {
        return top_ptr == &contents[STACK_SIZE];
    }
    
    void push(int i)
    {
        if (is_full())
            stack_overflow();
        else
            *top_ptr++ = i;
    }
    
    int pop(void)
    {
        if (!is_empty())
        return *--top_ptr;
        else
            stack_underflow();
    
        return 0;
    }
    
    void stack_overflow(void)
    {
      printf("Stack Overflow!\n");
      exit(EXIT_FAILURE);
    }
    
    void stack_underflow(void)
    {
      printf("Stack Underflow!\n");
      exit(EXIT_FAILURE);
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Your stack appears to work fine. The problem is related to the code in main(), where you do the read.

    getchar() returns an int, which will represent the character value as entered by the user. You need to check if each character is a number, then convert it to an int, before adding it to the stack.

    Code:
    while ((temp = getchar()) != '\n')
    {
    	if (isdigit(temp)) 
    	{
    		temp = temp - '0';
    		push(temp);
    	}
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Unregistered
    Guest
    I don't understand.
    If getchar returns an int, and I want it to be an int, and the person inters an int then why do I need to check if it is an int and convert it to an int?

    Can you please show me exactly how to do it?

    I have been looking at this code for so long and I just cant find the problem

    One reason I don't understand why I need to check if it is an int is because I have done similar programs before and it works like this..... I am sure i'm missing something so please show me what it is as I have gone a complete blank

    Thankyou

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When the user enters this
    >123<enter>
    the first character you program will see is the 1. Even though it's a 1 as far as the users see, the program will see it as it's ASCII equivalent (I assume your on a PC/Unix enviroment).

    Look at the ASCII chart and you'll see that 1 is actually 0x31.

    So, you must convert the character '1' (as entered by the user) to an int 1, to give you what you want. Do this by using the code I gave in my last post (there are other ways too, but this one is simplest for converting single characters, imo).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Unregistered
    Guest
    So I need to right isdigit()?
    I hope I get this done ok... i'll see what I can do and post what happens.
    Thankyou

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Unregistered
    So I need to right isdigit()?
    I hope I get this done ok... i'll see what I can do and post what happens.
    No, it's a library function. Just include
    >#include <ctype.h>

    Read the man page here
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Unregistered
    Guest
    Thankyou I finally made it work

    I have just a few more questions.

    Does this mean getchar() returns a char and I am "converting it" to an int? (It's called getchar so I guess thats what it does...)

    And, what "exactly" does the line
    temp = temp - '0';
    Do?

    Is this the prefered way to do it or is there a better way?

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Does this mean getchar() returns a char and I am "converting it" to an int? (It's called getchar so I guess thats what it does...)
    It is called getchar, but it returns an int because it should also be able to return EOF, which is not a value that can be held by a char. It is all very confusing in this area, just remember that getchar returns the character set's integer value for that character. Meaning that if you entered 5 at the keyboard, getchar would return 53 for ASCII instead of 5.

    >And, what "exactly" does the line
    >temp = temp - '0';
    >Do?
    temp - '0' returns the integer value of the character contained in temp. So if temp is '5' then temp - '0' will return the integer value 5 instead of the ASCII value 53 or the EBCDIC value 245.

    >Is this the prefered way to do it or is there a better way?
    It is the easiest way for single digit numbers.

    -Prelude
    My best code is written with the delete key.

  9. #9
    Unregistered
    Guest
    Thanks, I think I understand that ok...

    >It is the easiest way for single digit numbers.
    What about larger numbers? What's another way to do it?

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >What about larger numbers? What's another way to do it?
    There are several standard library functions which will do this with multidigit numbers. Though they assume that such numbers are in a char array:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( void )
    {
      int intVal = 0;
      char str[] = "12345";
      /*
      ** One way by using atoi
      */
      intVal = atoi ( str );
      printf ( "%d\n", intVal );
      /*
      ** Another with sscanf.
      */
      intVal = 0;
      if ( sscanf ( str, "%d", &intVal ) == 1 )
        printf ( "%d\n", intVal );
      return 0;
    }
    There are more, but you can find them all over the place so I won't reproduce them here.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. confused about system stack
    By George2 in forum C Programming
    Replies: 4
    Last Post: 06-05-2006, 09:14 AM
  2. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  3. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  4. Stack
    By planet_abhi in forum C Programming
    Replies: 2
    Last Post: 04-12-2003, 05:22 AM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 06:36 PM

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