[英]printing from an array struct by reference
我使用了6个功能。 其中三个(读取)用于扫描,其他三个用于打印(写入)。我检查了读取功能是否正常工作,问题主要出在其中一个写入功能上。 问题是我从用户那里扫描整数,并将它们存储在结构数组中,但是在打印它们时,我只能得到最后输入的整数。
#include<stdio.h>
#include<string.h>
typedef struct
{
char month[20];
int day;
int year;
} date_t;
typedef struct{
int hours;
int minutes;
int seconds;
}time_t;
typedef struct
{
char event[20];
time_t tm;
date_t dt;
}event_t;
int wr_event(const event_t*);
int wr_date(const event_t*);
int wr_time(const event_t*);
int rd_event(event_t*);
int rd_date(event_t*);
int rd_time(event_t*);
int main()
{
int i,j=0,temp=1;
event_t ev[5];
while(temp!=0&&j!=5)
{
rd_event(&ev[j]);
temp=strcmp(ev[j].event,"exit");
j++;
}
if(temp==0)j=j-1;
for(i=0;i<j;i++){
wr_event(&ev[i]);
}
}
int rd_time(event_t *ev)
{
printf("hours->");
scanf("%d",&ev->tm.hours);
printf("minutes->");
scanf("%d",&ev->tm.minutes);
printf("secondes->");
scanf("%d",&ev->tm.seconds);
}
int rd_date(event_t *ev)
{
printf("day-> ");
scanf("%d",&ev->dt.day);
fflush(stdin);
printf("month->");
gets(ev->dt.month);
printf("year->");
scanf("%d",&ev->dt.year);
}
int rd_event(event_t *ev)
{
printf("\nevent name->");
fflush(stdin);
gets(ev->event);
if(strcmp(ev->event,"exit")!=0){
rd_time(&ev);
rd_date(&ev);
}
}
int wr_time(const event_t *ev)
{
printf("this is the time of the event->%d %d %d\n\n",ev->tm.hours,ev->tm.minutes,ev->tm.seconds);
}
int wr_date(const event_t *ev)
{
printf("this the date of the event-> %d %s %d\n\n",ev->dt.day,ev->dt.month,ev->dt.year);
}
int wr_event(const event_t *ev)
{
printf("\nthis is your event-> %s\n\n",ev->event);
wr_time(&ev);
wr_date(&ev);
}
只是一个猜测-您的wr_time / date收到一个event_t *类型,但是您似乎将它们传递给event_t **(采用event_t *并使用&ev发送它的地址)。
该代码是否为您编译?
编辑:使用gcc编译了上面的代码,这是输出-
test.c: In function ?rd_event?:
test.c:78: warning: passing argument 1 of ?rd_time? from incompatible pointer type
test.c:79: warning: passing argument 1 of ?rd_date? from incompatible pointer type
test.c: In function ?wr_event?:
test.c:96: warning: passing argument 1 of ?wr_time? from incompatible pointer type
test.c:97: warning: passing argument 1 of ?wr_date? from incompatible pointer type
/tmp/ccwwiaHg.o: In function `rd_date':
test.c:(.text+0x19d): warning: the `gets' function is dangerous and should not be used.
所以至少我的编译器同意我的观点。 我对您的语言不熟悉,但是假设它会说另一种语言,我很难
尝试此操作,请注意指针也是变量的地址,因此向您传递了地址的地址..这就是问题的原因。 还看一下scanf的功能,以及如何排列括号
#include <stdio.h>
#include <string.h>
typedef struct
{
char month[20];
int day;
int year;
}date_t;
typedef struct{
int hours;
int minutes;
int seconds;
}timeT;
typedef struct
{
char events[20];
timeT tm;
date_t dt;
}event_t;
int wr_event(const event_t*);
int wr_date(const event_t*);
int wr_time(const event_t*);
int rd_event(event_t*);
int rd_date(event_t*);
int rd_time(event_t*);
void main()
{
int i,j=0,temp=1;
event_t ev[5];
char tmp;
while(temp!=0&&j!=5)
{
rd_event(&ev[j]);
temp=strcmp(ev[j].events,"exit");
j++;
}
if(temp==0)j=j-1;
for(i=0;i<j;i++){
wr_event(&ev[i]);
}
printf("press any key to exit");
scanf("%c",&tmp);
}
int rd_time(event_t *ev)
{
printf("hours->");
scanf("%d",&(ev->tm.hours));
printf("minutes->");
scanf("%d",&(ev->tm.minutes));
printf("secondes->");
scanf("%d",&(ev->tm.seconds));
}
int rd_date(event_t *ev)
{
printf("day-> ");
scanf("%d",&(ev->dt.day));
fflush(stdin);
printf("month->");
gets(ev->dt.month);
printf("year->");
scanf("%d",&(ev->dt.year));
}
int rd_event(event_t *ev)
{
printf("\nevent name->");
fflush(stdin);
gets(ev->events);
if(strcmp(ev->events,"exit")!=0){
rd_time(ev);
rd_date(ev);
}
}
int wr_time(const event_t *ev)
{
printf("this is the time of the event->%d %d %d\n\n",ev->tm.hours,ev- >tm.minutes,ev->tm.seconds);
}
int wr_date(const event_t *ev)
{
printf("this the date of the event-> %d %s %d\n\n",ev->dt.day,ev->dt.month,ev->dt.year);
}
int wr_event(const event_t *ev)
{
printf("\nthis is your event-> %s\n\n",ev->events);
wr_time(ev);
wr_date(ev);
}
如果用户输入数字,数字或字符串,您还需要进行校验,否则porgram会发疯。 您还需要检查很多事情,例如用户输入的字母是否多于20个,等等。 创建与用户通信的程序时,必须检查输入是否有效!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.