user3125530
user3125530

Reputation: 59

cannot change value in struct using pass by reference

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!

Upvotes: 2

Views: 1483

Answers (2)

Dilip Kumar
Dilip Kumar

Reputation: 1746

  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;
}

Upvotes: 0

ma08
ma08

Reputation: 3744

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.

Upvotes: 3

Related Questions