2

So, I wanted to create a stack as follows:

#include <stdio.h>
#include <stdlib.h>

typedef struct Node{
        int data;
        struct node *link;
}node;

typedef struct Stack{
        struct Node *topnode;
        int count;
}stack;

void push(int data, stack *ourstack){
    node newnode;
    newnode.data = data;
    ourstack.topnode = &newnode;
}

int main()
{
    stack mystack;
    push(1,mystack);
    printf("%d",(mystack.(*topnode).data));


}

but I do get errors. I am a bit confused here. Inside the push() function, in the last line, I tried various ways of implementing it right but I failed each time. Now, my thinking is, ourstack is a pointer pointing to a struct Stack. And the topnode is also a pointer inside a structure of a stack which points to another node structure. So, should not (*ourstack).(*topnode) = newnode or ourstack.topnode = &newnode work? Why?

4
  • "I do get errors" - what errors? Commented Feb 1, 2018 at 13:49
  • request for member 'topnode' in something not a structure or union in ourstack.topnode = &newnode @JohnZwinck Commented Feb 1, 2018 at 13:50
  • 1
    struct Node -> struct node , ourstack.topnode -> ourstack->topnode, push(1, mystack); -> push(1, &mystack);, (mystack.(*topnode).data) -> mystack.topnode->data and a few more problems. Commented Feb 1, 2018 at 13:53
  • 3
    Hint: open your C text book and read the chapters dealing with dynamic memory allocation and with pointers. Commented Feb 1, 2018 at 13:55

3 Answers 3

5

Neither work because newnode is on the stack and once the code exits push, it will no longer exist. You need to allocate it dynamically.

void push(int data, stack *ourstack){
    node *newnode;
    newnode = malloc(sizeof(*newnode));
    newnode->next = ourstack->topnode; // Point the next of the new node to the top node of the stack
    newnode->data = data;
    ourstack->topnode = newnode;
}

And also you need to initialise mystack properly in main or else you risk undefined behaviour as topnode could be NULL or it could have a random value.

stack mystack;
mystack.topnode = NULL;
mystack.count = 0;
Sign up to request clarification or add additional context in comments.

8 Comments

I see, and should it be push(1,mystack)?
no - mystack is declared as stack and push is expecting a stack * - a pointer to stack, so you need to call it push(1,&mystack)
And printing the value uses mystack.topnode->data
you use . for structs and -> for pointers to structs. You shouldn't need to use * or & in such cases as it makes the code unreadable
It isn't a struct node though - it's a struct Node or it's node - that's the problem with using different capitalisation of the names - you should stick to just one style to prevent such confusion
|
2

variable in a method, only exists in stack, will be unavailable after exit method. since you wish keep it after call of push, you need alloc a node with function like malloc, which will save data in heap. and you have to free it after you do not need it any more.

Comments

2

There are number of bugs in your program.

  • ourstack is pointer variable of structure type, while accessing data using ourstack use -> operator instead of dot operator

    Replace

    ourstack.topnode = &newnode;
    

    with

    ourstack->topnode = &newnode;
    
  • there is a conflict in push function definition and function call, you are passing just mystack and catching with struct pointer variable. pass the address of mystack, your function call should be

    push(1,&mystack);
    
  • newnode is the local variable declared in push function and whatever you are trying with newnode it will reflects in this function only, not outside of this. So Allocate memory dynamically for newnode and do the operation.

    void push(int data, stack *ourstack){
        node *newnode;
        newnode = malloc(sizeof(stack));
        newnode->data = data;
        ourstack->topnode = newnode;
    }
    

Once compiled successfully debug the code using gdb and analyze.

2 Comments

So, if I want to access to the first element of the stack, should I be doing mystack.topnode->data or mystack->topnode.data ? because neither works when I want to print it printf("%d",mystack.topnode->data);
simply do printf("%d\n", (mystack.topnode)->data);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.