你能幫我將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;
}
你從編譯器得到什麼錯誤? – xFighter
@xFighter它只是不打印 – Nikitax
使用'viod show_info_abt_bus(info_bus *);'作爲函數簽名。 – bansi