2

Hi everyone I am now implementing a dynamic size stack in C but get stuck in a problem... This is part of my code:

#include <stdio.h>
#include <conio.h>

typedef struct Node node;

struct Node{    
  int value;
  node *above;
};

struct stack{
    node *root;
    node *top;
};

typedef struct stack stack;

void stack_init(stack *s){
    s = (stack*) malloc(sizeof(stack));
    s->top=-1;
    s->root=-1;
    printf("%d %d\n", s->top, s->root);
};

int main(){
    stack s;
    stack_init(&s);
    printf("%d %d\n", s.top, s.root);
}

When I run the code the printf from stack_init give -1 -1 and the one from main give 70 8, while in my assumption they should be the same. What is the problem? Any help is appreciated!

4
  • Probably because you're assigning the pointed a memory location allocated with malloc rather than the value the user passes in. Commented Nov 29, 2015 at 10:41
  • That's because you are not modifying the original s buffer passed into stack_init but rather a new buffer allocated by malloc. Just remove the malloc line and it will do what you want. One note: It's not a good idea to init pointers to -1 as that is not a valid pointer value. Instead use NULL. Commented Nov 29, 2015 at 10:41
  • just remove the line s = (stack*) malloc(sizeof(stack)); Commented Nov 29, 2015 at 10:43
  • You don't seem to understand that you can't pass by reference in C. Commented Nov 29, 2015 at 12:15

2 Answers 2

3

You are changing the pointer value when passing to the function.

void stack_init(stack *s){
s = (stack*) malloc(sizeof(stack));

This doesn't work because, you are assigning it to the pointer s which is in the context of the function stack_init. It has no effect on the pointer you passed from the main. C is pass by value. So the pointer's value(address is present in s) is the address of the vairable in main, when you assign s to something it gets assigned to a different address, having no effect on the stack variable in the main function.

So you need a double pointer.

void stack_init(stack **s){

   *s = (stack*) malloc(sizeof(stack));
   (*s)->top=-1;
   (*s)->root=-1;
   printf("%d %d\n", (*s)->top, (*s)->root);

};

int main(){
        stack *s;
        stack_init(&s);
        printf("%d %d\n", s->top, s->root);

}

Or you can return a pointer from the stack_init function and return it so that you can assign it to your stack pointer. Or you can allocate the memory in the main function and pass the variable to the function to intialize the values.

There are many ways to do it, it just depends on your use case. I have given code to do it in one way.

Sign up to request clarification or add additional context in comments.

Comments

0
  1. Problem with malloc, here you are overwriting the value of s with one more location in memory using malloc and updating values in that location and also printing in that function which is local to that function.
  2. In main you are printing the actual values of s which was not modified in any way in the function stack_init.
  3. minor warnings No return in the main function.
  4. No typecasting for updating the values of Node. you are assigning the integer value for a structure and also while printing also format specifiers are not suitable for the values printing.

Here is the code with modifications

#include <stdio.h>
#include <stdlib.h>
typedef struct Node node;
struct Node{

  int value;
  node *above;
};

struct stack{

        node *root;
        node *top;

    };
    typedef struct stack stack;

void stack_init(stack *s){

//    s = (stack*) malloc(sizeof(stack));
    s->top=(node*)-1;
    s->root=(node*)-1;
    printf(" loop %d %d\n", s->top, s->root);
};


int main(){
        stack s;
        stack_init(&s);
        printf("%d %d\n", s.top, s.root);
        return 0;
}

Comments

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.