2016-12-25 58 views
0

你能幫我將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; 
} 
+0

你從編譯器得到什麼錯誤? – xFighter

+0

@xFighter它只是不打印 – Nikitax

+0

使用'viod show_info_abt_bus(info_bus *);'作爲函數簽名。 – bansi

回答

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; 
} 
+0

如果n不是2?或10,20,25等? – Nikitax

+0

@Nikitax我只用n = 2進行測試。你可以傳遞任何數字。 –

+0

但我該如何在功能中聲明'n'?它就像'void show_info_abt_bus(info_bus *,int);'和'void show_info_bus_abt_bus(info_bus * b,int n)'和'show_info_bus_abt_bus(b,n);' – Nikitax