如何为结构创建打印功能? [英] How do I creat a print function for structure?

查看:48
本文介绍了如何为结构创建打印功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试为结构制作打印功能,但遇到了我以前没看过的错误

这是我的鳕鱼:





I try to make a print function for structure but meet a errow I have not seem before
this is my cod:


   #include<stdio.h>
   struct point{
	int x;
	int y;
   }p;
   
    int main()
   {
        struct point scan(struct point *p);
	void print(struct point *p);
	
		
	scan(&p);
	print(&p);
	
	 
	
	return 0;
}
     struct point scan(struct point *p)
{
	scanf("%d%d",p->x,p->y); 
}

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





我尝试过:



我搜索了谷歌但没有发现什么。

顺便说一下,如果你认为我的问题很愚蠢,请为我推荐一些书,我会很感激的。



What I have tried:

I have searched google but nothing found nothing.
By the way,if you think my question is stupid,please recommend some book for me, I will appreciate that.

推荐答案

你不能在函数体内声明函数原型。将这些行移到 main 的定义之上:

You cannot declare function prototypes inside the body of a function. Move these lines above the definition of main:
struct point scan(struct point *p);
	void print(struct point *p);





并帮自己一个忙,不要使用单个字符名称:使用正确的名称,你的代码变得更容易阅读。



And do yourself a favour, and don't use single character names: use "proper" names and your code becomes a lot easier to read.

#include<stdio.h>
struct point
   {
   int x;
   int y;
   } myPoint;

struct point scan(struct point *p);
void print(struct point *p);
   
int main()
   {
   scan(&myPoint);
   print(&myPoint);
   return 0;
   }
   
struct point scan(struct point *p)
   {
   scanf("%d%d",p->x,p->y); 
   }
void print(struct point *p)
   {
   printf("%d%d",p->x,p->y);
   }


这篇关于如何为结构创建打印功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆