c - 请检查我的 segFault

标签 c pointers segmentation-fault

我在这里遇到了段错误。我很困惑。 请帮帮我。 f1 和 y 都是结构体节点的指针。 我想把 y 的左转 f1 右转。

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

最佳答案

y=f1->righty 设置为未初始化的内存。 y->left 现在无效。尝试使用 Valgrind 运行它。

关于c - 请检查我的 segFault,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26069396/

相关文章:

检查是否在 X Window 中运行

c - 在 C 中,结构数组如何在内存中查找

c++ - <未解析的重载函数类型> 尝试将聚合对象的方法传递给其类方法时

c - 内存会被破坏吗?

c++ - 将一个指针分配给另一个指针时出现段错误

c - 在c中使用指针创建数组

c - 启用从用户空间进程调试内核模块

c - 在 C 中,如何在没有指针的情况下更改局部范围变量?

c - 流缓冲区大小小于输入流,但没有立即段错误

c - 复制一个字符串数组(或将它们复制到另一个数组)?