struct in function和output in a file

时间:2016-12-25 11:39:52

标签: 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;
}

1 个答案:

答案 0 :(得分:1)

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

#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;
}