Litomas 2019-07-10 08:57 采纳率: 50%
浏览 253
已采纳

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

  • 写回答

2条回答 默认 最新

  • threenewbee 2019-07-10 09:15
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突