Thread: does anyone know how to pop off a stack?

  1. #1
    Registered User
    Join Date
    Sep 2005

    does anyone know how to pop off a stack?

    I have a linked list, that I want to remov things off the top like a stack....

    I made this function but it doesn't work...

    does anyone know what I'm doing wrong?

    STUDENT* remv_student( STUDENT* ptr ) {                
            static STUDENT popped_student;        
             popped_student = *top;                
             ptr = top;
             top = top -> studentPtr;
             free (ptr);
             return &popped_student;

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Banks of the River Styx
    First, top is never defined. I'm going to assume that it's a global variable. I'd name it something a little more... informative, myself. "student_list", etc.

    Your code seems fine to me. (Though no error checking for end of list.) However, why return a pointer? (To a variable inside a function... although static, seems a fishy thing to do...) Couldn't you just return the structure?

    Otherwise, is top (and it's members) valid? What exactly "doesn't work"? Does it crash? If so, where?
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Yeah, I agree. You shouldn't be using the static variable at all. Just return the node you're popping. Oh, also, you can't update a what a pointer points at inside another function unless you're passing a pointer to the pointer you want to update. It should then be something like:
    STUDENT *pop( STUDENT **stack )
        STUDENT *n = NULL;
        if( students_enrolled )
            n = *stack;
            *stack = *stack->next;
        return n;
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 02:56 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM