JcKelley
JcKelley

Reputation: 1994

C Stack pointing to Address?

I am new to C. I have implemented a simple stack with some structs and what not. I have posted the entire code below. The problem section is commented.

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>

typedef struct Node{
    int data;
    struct Node *next;
} Node;
typedef struct Stack{
    Node *top;
    int size;
} Stack;

/* Function Prototypes */
void push(Stack *sPtr, int data);
int pop(Stack *sPtr);
void create(Stack *sPtr);

int main(void)
{
    static Stack first;
    create(&first);

    push(&first,4);
    push(&first,3);
    push(&first,2);

    printf("%d\n",pop(&first));
    printf("%d\n",pop(&first));
    printf("%d\n",pop(&first));
    exit(1); 
}

void push(Stack *sPtr, int data)
{
    struct Node newNode;
    newNode.data = data;
    newNode.next = sPtr->top;
    sPtr->top = &newNode;
    sPtr->size++;
    printf("%d\n",sPtr->top->data);
}
int pop(Stack *sPtr)
{
    struct Node *returnNode = sPtr->top;
    struct Node *topNode = sPtr->top;
    if(sPtr->size != 0){
        sPtr->top = topNode->next; /* =============PROBLEM?=============== */
        return returnNode->data;
    }
    else{
        printf("Error: Stack is Empty!\n");
        return -1;
    }
}
void create(Stack *sPtr)
{
    sPtr->size = 0;
    sPtr->top = NULL;
}

The output of this code is

4
3
2
2
8103136
680997

So obviously, it is pulling off the top node, and then printing the addresses of the next two nodes, instead of their data.

But why is it doing this? As far as I know (which is little) preforming this operation

sPtr->top = topNode->next;

should tell the program to make top now point to to topNode.next. But instead, it seems to be returning the address. What's going on here?

Upvotes: 1

Views: 232

Answers (2)

Jonathan Leffler
Jonathan Leffler

Reputation: 754090

One trouble is that pop() never decrements size, so size is really 'number of elements ever pushed onto stack', not 'the number of elements in the current stack'.

int pop(Stack *sPtr)
{
    struct Node *returnNode = sPtr->top;
    struct Node *topNode = sPtr->top;
    if (sPtr->size != 0)
    {
        sPtr->top = topNode->next;
        sPtr->size--;
        return returnNode->data;
    }
    else
    {
        fprintf(stderr, "Error: Stack is Empty!\n");
        return -1;
    }
}

Another trouble, as pointed out by unluddite in his answer is that you are not pushing data correctly. You need both fixes to be safe. There might still be other problems (such as not freeing memory correctly — or at all), but these two will get you a long way.

Upvotes: 3

Eugene S
Eugene S

Reputation: 3122

In your push() function, you're creating a new struct Node and adding it to your stack. However, the node is a local variable within the scope of push()--allocated on the stack (not your stack, the call stack), and will be gone when push() returns.

What you want to do is create the node on the heap, which means it will still be there after push() returns.

Since you're coding in C, you'll want to do something like:

struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));

Since you're now dealing with heap-allocated memory, you'll need to make sure that at some point it gets freed (somewhere) using free().

You're also not decrementing size as Jonathan has pointed out.

Upvotes: 4

Related Questions