c++ - 这段 C++ 代码有什么问题?它在运行时崩溃

标签 c++ pointers nodes

我想知道下面的 C++ 代码有什么问题。它在运行时崩溃。

#include <iostream.h>
#include <conio.h>
using namespace std;

class node
{
    public:
        int info;
        node *addr;

        node(){
            info = 0;
            addr = NULL;          
        }

        ~node(){}
};

void func(node *);


int main(void){   
    node *head;
    node b;

    b.info = 10;

    *head = b;

    func(head); 
    getch();
}

void func(node *obj){
    cout<<"i: "<<(*obj).info;
}

最佳答案

*head = b;

不正确,因为 head 没有指向任何东西

head = &b ;

应该解决问题

关于c++ - 这段 C++ 代码有什么问题?它在运行时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13023956/

相关文章:

c++ - 通过 std::cin 在一行中读取 500 个输入会产生垃圾

c++ - 使用未声明的标识符 'FunctionName' C++ Xcode

c++ - 如何在 Eclipse 中为启动新项目时创建的文件设置 C++ 文件扩展名为 .cc?

c++ - 在 Itanium C++ ABI 中,为什么模板函数的错误名称无法解析依赖的 typedef?

C 指向数组/指针数组的指针消歧

C : Why isn't my global variable updating?

c - 如何连接 char* 字符串?

algorithm - 两个网络图的并集

ios - Sprite Kit - 从 didBeginContact() 创建节点

javascript - insertRule() 不插入规则但没有给出任何错误