c - 函数中的结构和文件中的输出

标签 c struct printf malloc

您能帮我将 struct 的元素传递到函数 (void show_info_abt_bus) 中以输出我的信息吗?我不明白应该如何传递这些元素。

   #include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>

typedef struct info_bus_{
    int number;
    int begin;
    int end;
    char* stations;
    int time_working;
}info_bus;

void input_data_abt_bus(info_bus **,int);
void show_info_abt_bus(info_bus *,int);
void my_free(info_bus **,int);



void input_data_abt_bus(info_bus **b,int n){

    int i;
    char buffer[128];


   if(!((*b)=(info_bus *)malloc(n*sizeof(info_bus)))){
    printf("Error memory\n");
    exit(0);
   }

    for(i=0;i<n;i++){
    printf("Input the number of a bus: \n");
    scanf("%d",&((*b)[i].number));
    printf("%d)Input when it starts to work: \n",i+1);
    scanf("%d",&((*b)[i].begin));

    printf("%d)Input when it  finishes to work: \n",i+1);
    scanf("%d",&((*b)[i].end));

        printf("%d)Input its stations: \n",i+1);
        scanf(" %127[^\n]%*c", buffer);
        (*b)[i].stations = (char*) malloc(strlen(buffer) + 1);
        strcpy((*b)[i].stations, buffer);
        getchar();

    printf("Input time working: \n");
    scanf("%d",&((*b)[i].time_working));
        }
}

void my_free(info_bus **b,int n){
    int i;
   for (i = 0; i < n; i++) {
        free((*b)[i]);
    }
    free(b);
}

int main()
{
    int i,n;
    printf("How many buses u have: \n");
    scanf("%d",&n);
    info_bus *b=NULL;
    input_data_abt_bus(&b,n);
    show_info_bus_abt_bus(b,n);
    my_free(b,n);

    return 0;
}

最佳答案

您需要在函数中通过引用传递结构体对象。

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>

typedef struct info_bus_{
    int number;
    int begin;
    int end;
    char* stations;
    int time_working;
}info_bus;

void input_data_abt_bus(info_bus **,int);
void show_info_abt_bus(info_bus *,int);

void show_info_bus_abt_bus(info_bus *b,int n){
 int i;
  for (i=0;i<n;i++){
    printf("\n===============================================");
    printf("\n[%d].the number of a bus: %d",i+1,b[i].number);
    printf("\n[%d]. Begin at: %d am",i+1,b[i].begin);
    printf("\n[%d]. Finishes at: %d pm",i+1,b[i].end);
    printf("\n[%d]. Stations: %s",i+1,b[i].stations);
    printf("\n[%d]. Time working: %d",i+1,b[i].time_working);
    printf("\n===============================================\n");
    }
}

void input_data_abt_bus(info_bus **b,int n){

    int i;
    char buffer[128];


    (*b)=(info_bus *)malloc(n*sizeof(info_bus));

    for(i=0;i<n;i++){
    printf("Input the number of a bus: \n");
    scanf("%d",&((*b)[i].number));
    printf("%d)Input when it starts to work: \n",i+1);
    scanf("%d",&((*b)[i].begin));

    printf("%d)Input when it  finishes to work: \n",i+1);
    scanf("%d",&((*b)[i].end));

        printf("%d)Input its stations: \n",i+1);
        scanf(" %127[^\n]%*c", buffer);
        (*b)[i].stations = (char*) malloc(strlen(buffer) + 1);
        strcpy((*b)[i].stations, buffer);
        getchar();

    printf("Input time working: \n");
    scanf("%d",&((*b)[i].time_working));
        }
}
void my_free(info_bus **b,int n)
{
     int i;
     for (i = 0; i < n; i++) {
         free((*b)[i].stations);
     }
     free((*b));
     (*b)=NULL;
}

int main()
{
    int i,n;
    printf("How many buses u have: \n");
    scanf("%d",&n);
    info_bus *b=NULL;
    input_data_abt_bus(&b,n);

    show_info_bus_abt_bus(b,n);
    my_free(&b,n)

    return 0;
}

关于c - 函数中的结构和文件中的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41320812/

相关文章:

c - 简单的 if 语句和 scanf 不适合我

c - 通过引用将静态二维结构数组传递给函数

c - 如何找到C语言中printf的源代码?

c++ - 结构成员的模板函数

C数学不尊重声明的常量

c++ - %ld 格式转换以实现便携性

java - 我怎样才能在我的java上使用c函数

c - 了解 C 中的 fork() 顺序

c - C 中结构体定义的变体

c - 在 AVR 中将结构体设置为数组