C++中一个与指针作为函数参量的问题。

两种写法都莫得输出,求大神帮忙看看问题在哪?

两种写法不同的地方就是 Print() 这个函数的参量的传入方式。

#include<bits/stdc++.h>
using namespace std;

typedef struct node * Node;
struct node
{
    int data;
    Node next;
};


int Print(Node &h)
{
    return h->data;
}

int main()
{
    Node h = NULL;
    h->data = 1;
    h->next = NULL;
    int t = Print(h);
    printf("%d\n",t);
    return 0;
}

------------------------**********************
这是第二种写法

#include<bits/stdc++.h>
using namespace std;

typedef struct node * Node;
struct node
{
    int data;
    Node next;
};


int Print(Node h)
{
    return h->data;
}

int main()
{
    Node h = NULL;
    h->data = 1;
    h->next = NULL;
    int t = Print(h);
    printf("%d\n",t);
    return 0;
}

h没有初始化怎么就访问了
Node h = NULL;
->
Node h = (Node)malloc(sizeof(node));
或者
struct node node1;
Node h = &node1;

问题在main函数,用Node定义的h是一个指针,并没有开辟struct node类型的结构体空间,都没有内存空间,用指针又怎么能访问到结构体成员的数据?
所以首先得定义一个结构体变量,再用h指向这个变量,这样就可以访问data和next了。