Thread: Trying to make a stack through malloc

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    24

    Trying to make a stack through malloc

    I am curious as to what is happening to my pointers and everything once I call malloc to create space for 5 integers to try to make a stack.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    void add(int * TOP, int * stack);
    
    
    int main()
    {
        int *stack = NULL;
        int *TOP = NULL;
        stack = (int *)malloc (5 * sizeof(int));
        TOP = stack;
        int b = 0; 
        int (*f[])(int * TOP ,int * stack) = {add};
            
         while (b == 0){
        
        printf("What would you like to do? Press 0 to stack.");
        scanf("%d", &b);
        printf("%d",f[b]( TOP, stack));
        printf("Please enter another number.");
        scanf("%d", &b);
        }    
        return 0;
    }
    
    
    void add(int * TOP, int * stack)
    {
           int a = 0;
           
           printf("Please enter a number to stack.");
           scanf("%d", &a);
           if (TOP == stack + 5)
          printf("The stack is full.");
             else{
                *TOP = a;
                printf("The number put on the stack was %d", *TOP);
                TOP++;
            }
        
    }
    I am guessing that when I initialize stack to malloc, stack now stores the starting address of where the space is taken out, and also assigns TOP that adress too. I get the choice from the user (b) to get the instruction to try to push on the stack. I was told that the if statement in the function checks if the stack has passed the bounds of 5 elements. If not, it assigns the scanned variable and puts it into what TOP is pointing to and increments TOP to the next address. It is not working and am wanting to see where my logic is wrong, because this is all very confusing to me. Thank you.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    A function cannot change the value of a parameter passed into it, so any changes made to TOP inside add (such as "TOP++") vanish when the function ends.

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    24
    Other than that, any other obvious logic errors? When I try to print out the value that is given after putting it into *TOP it is not the right number for some reason.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    My compiler tells me
    warning: initialization from incompatible pointer type
    because you claim the function pointer returns an int, but the function itself is void. Then you try to print the return value of the function which doesn't exist. Just call the function, don't print it; and you'll need to decide what it should return and be consistent.

  5. #5
    Registered User
    Join Date
    Oct 2013
    Posts
    24
    Alright, I got a little bit of it to work by putting this in.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define SIZE  5;
    int add(int * TOP, int * stack);
    
    
    int main()
    {
        int *stack = NULL;
        int *TOP = NULL;
        stack = (int *)malloc (5 * sizeof(int));
        TOP = stack;
        int b = 0; 
        int (*f[])(int * TOP ,int * stack) = {add};
        
        
        printf("What would you like to do? Press 0 to stack.");
        scanf("%d", &b);
        
         while (b == 0){
        
        
         (*f[b])(TOP ,stack);
         TOP ++;
         
         printf("Please enter another number.");
         scanf("%d", &b);
        }    
        return 0;
    }
    
    
    int add(int * TOP, int * stack)
    {
           int a = 0;
           
           printf("Please enter a number to stack.");
           scanf("%d", &a);
           if (TOP == stack + 5)
          printf("The stack is full.");
             else{
                *TOP = a;
                printf("The number put on the stack was %d\n", *TOP);
               
                return *TOP;
            }
        
    }
    Last edited by sammythesp3rmy; 10-27-2013 at 01:02 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I make a constructor make code fail?
    By MutantJohn in forum C++ Programming
    Replies: 29
    Last Post: 08-31-2013, 06:59 PM
  2. Replies: 2
    Last Post: 04-27-2011, 04:14 PM
  3. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  4. Edit stack code to make queue
    By mikeman in forum C++ Programming
    Replies: 1
    Last Post: 02-15-2010, 09:49 AM
  5. stack make file problem
    By puckett_m in forum C Programming
    Replies: 2
    Last Post: 11-22-2001, 11:51 AM