原来是菜菜捏 2022-10-14 10:42 采纳率: 0%
浏览 20

C语言结构,指针,函数

问题遇到的现象和发生背景

MOOC上翁恺的C语言进阶课,3.2.2结构与函数,完全是抄的代码,不知道为什么跑不出来结果

用代码块功能插入代码,请勿粘贴截图
#include <stdio.h>

struct point { //定义结构全局变量
    int x;
    int y;
};

struct point* getStruct (struct point*);//函数声明
void output(struct point);
void print(const struct point *p);

int main()
{
    struct point y = {0,0}; 
    getStruct(&y);
    output(y);
    output(*getStruct(&y));        
    print(getStruct(&y));
    
    return 0;
}

struct point* getStruct (struct point *p)//参数是指针 函数用于读入结构
{
    scanf("%d",&p->x);
    scanf("%d",&p->y);
    printf("1.%d,%d\n",p->x,p->y);
    return p;
}

void output(struct point p)        //输出结构
{
    printf("2.%d,%d\n",p.x,p.y);    
}

void print(const struct point *p)    //用指针输出结构
{
    printf("3.%d,%d\n",p->x,p->y);
}

运行结果及报错内容

输入56 77
输出
1.56,77
2.56,77
可以编译通过没有报错,运行时只输出了两次,第二个output函数和print函数没有输出内容

我想要达到的结果

按理说应该将结构输出四次的,但是只输出了两次。C语言初学者,希望自己没有提出太nc的问题,求指点

  • 写回答

4条回答 默认 最新

  • [Pokemon]大猫猫 2022-10-14 10:51
    关注

    题主按你的代码,遇到getstruct就需要输入数据,所以可能要三次输入

    评论

报告相同问题?

问题事件

  • 创建了问题 10月14日

悬赏问题

  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价
  • ¥15 在anaconda上pytorch和paddle paddle下载报错
  • ¥25 自动填写QQ腾讯文档收集表