-2

I got the seg fault in here. I confused. Please help me out. f1 and y are both pointer for struct node. I want to turn y's left to f1 right.

#include <stdio.h>
#include <stdlib.h>
struct node{
            int data;
              struct node* left;
              struct node* right; 
};
int main(){
    struct node* f1=(struct node *)malloc(sizeof(struct node));
    struct node* y=(struct node *)malloc(sizeof(struct node));
    f1->data=10;
    y=f1->right;
    f1->right=y->left;   //seg fault is in this line. 

    return 0;
}`

1 Answers1

2

y=f1->right sets y to uninitialised memory. y->left is now invalid. Try running this with Valgrind.

apmasell
  • 7,033
  • 19
  • 28