运行时主函数最后的自定义函数print无法运行

# include <stdio.h>

struct point
{
  int x;
  int y;
};

struct point *getstruct(struct point *p)
{
  printf("first:");
  scanf("%d",&p->x);
  printf("second:");
  scanf("%d",&p->y);
  printf("%d,%d\n",&p->x,&p->y);
  return p;
}

void output(struct point p)
{
  printf("out:%d,%d\n",p.x,p.y);
}

void print(const struct point *p)
{
  printf("pri:%d,%d\n",p->x,p->y);
}

int main(int argc, char const *argv[])
{
  struct point y={0,0};
  getstruct(&y);
  output(y);
  output(*getstruct(&y));
  print(getstruct(&y));
}

运行时主函数最后的自定义函数print无法运行

可以运行,不过需要三次输入first、second。

img


原因如下:

    getstruct(&y);  //第一次进入getstruct
    output(y);
    output(*getstruct(&y));    //第二次进入getstruct
    print(getstruct(&y));    //第三次进入getstruct


#include <stdio.h>

struct point
{
    int x;
    int y;
};

struct point *getstruct(struct point *p)
{
    printf("first:");
    scanf("%d", &p->x);
    printf("second:");
    scanf("%d", &p->y);
    printf("%d,%d\n", p->x, p->y);
    return p;
}

void output(struct point p)
{
    printf("out:%d,%d\n", p.x, p.y);
}

void print(const struct point *p)
{
    printf("pri:%d,%d\n", p->x, p->y);
}

int main(int argc, char const *argv[])
{
    struct point y = {0, 0};
    getstruct(&y);
    output(y);
    // output(*getstruct(&y));
    print(getstruct(&y));
}