c - 通过引用从数组结构打印

标签 c

我使用了 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.

所以至少我的编译器同意我的观点。我不熟悉你的语言,但我很难假设它懂得不同的语言

关于c - 通过引用从数组结构打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18882639/

相关文章:

c - 在 C 代码中表示 EOF?

c - 绑定(bind):无法分配请求的地址

c - 如何将空指针(结构返回类型)更改为声明的结构?

c - 有没有办法在 strcmp 中使用 char?

c++ - 当 C 和 C++ 中严格要求内存释放时?

C代码中的CRLS归并排序边界代码理解

c - 如何在 C 中以任何预处理器指令形式定义一组配置

c - 为 size_t 切换为负值

c - 标准输入上的 ReadFile 在 Windows 7 上崩溃

C 向子进程发送信号父进程