2016-05-09 166 views
-1

我有一個C語法的小問題。我想建立一個結構數組。但之前返回C結構聲明

預期的聲明說明符或「...」「城市」

這裏是我的代碼

#include <stdio.h> 
#include <string.h> 
#include <fcntl.h> 
#include <sys/types.h> 
#include <sys/stat.h> 

struct City { 
    int plateNumber; 
    char name[32]; 
    double area; 
    int population; 
    int populationDensity; 
    int cityCenterPopulation; 
    char region[32]; 
    double latitude; 
    double longitude; 
}; 

struct City cities[81]; 
#define Struct_Size sizeof(struct City) 

void printCityData(cities[], int i); 
void readFile(); 

void readFile(){ 
    int fd = open("data.dat", O_RDONLY); 
    if(fd<0) { 
    printf("Error while opening file!"); 
    return; 
    } 
    int i; 
    for (i = 0; i < 81; i++) { 
    while(read(fd,&cities[i], Struct_Size)>0) { 
     printCityData(cities[i],i); 
    } 
    } 

} 

void printCityData(cities[], int i) { 
    printf("CITIES\n"); 
    printf("plateNumber: %d\n", cities[i].plateNumber); 
    printf("name: %s\n", cities[i].name); 
    printf("area: %lf\n", cities[i].area); 
    printf("population: %d\n", cities[i].population); 
    printf("populationDensity: %d\n", cities[i].populationDensity); 
    printf("cityCenterPopulation: %d\n", cities[i].cityCenterPopulation); 
    printf("region: %s\n", cities[i].region); 
    printf("latitude: %lf\n", cities[i].latitude); 
    printf("longitude: %lf\n", cities[i].longitude); 
} 

int main(int argc, char const *argv[]) { 
    readFile(); 
    return 0; 
} 

我怎樣才能解決這個問題?

+0

'無效printCityData(市[],int i)以{' - >'無效printCityData(int i)以{'' – BLUEPIXY

+0

的#define STRUCT_SIZE的sizeof(結構市)'至少是一個命名錯誤的宏,因爲它甚至不會引用哪個'struct'的意思。說,更好地完全刪除這個宏。不要太喜歡宏,特別是作爲初學者。 (也可以用'inline'函數代替) – Olaf

回答

3

我相信,這個問題是由於cities[]

void printCityData(cities[], int i) 

這應該是

void printCityData(struct City cities[], int i) 

記住,cities是一個變量,而不是數據類型丟失的數據類型。 FWIW,在前向聲明的情況下,實際上可以省略變量名稱而不是變量類型。

也就是說,實際上,cities是一個全球變量在這裏,你不需要作爲參數傳遞。您可以減少功能簽名

void printCityData(int i) { 

,並調用它像

printCityData(i); 
+0

'printCityData(cities [i],i);' - >'printCityData(cities,i);' – BLUEPIXY

+0

@BLUEPIXY對,您的任何一條評論_must_被尊敬。 :) –

+0

@Berkin所以我希望現在它的作品! :) –

0

的問題是,你沒有提到的城市的數據類型,你也分別致電用不同的數據類型的功能那我想你想要的。我做了必要的修改:

#include <stdio.h> 
#include <string.h> 
#include <fcntl.h> 
#include <sys/types.h> 
#include <sys/stat.h> 

struct City { 
    int plateNumber; 
    char name[32]; 
    double area; 
    int population; 
    int populationDensity; 
    int cityCenterPopulation; 
    char region[32]; 
    double latitude; 
    double longitude; 
}; 

struct City cities[81]; 
#define Struct_Size sizeof(struct City) 

void printCityData(struct City cities[], int i); //cities is an array of struct City 
void readFile(); 

void readFile(){ 
    int fd = open("data.dat", O_RDONLY); 
    if(fd<0) { 
    printf("Error while opening file!"); 
    return; 
    } 
    int i; 
    for (i = 0; i < 81; i++) { 
    while(read(fd,&cities[i], Struct_Size)>0) { 
     printCityData(cities,i); 
    } 
    } 

} 

void printCityData(struct City cities[], int i) { 
    printf("CITIES\n"); 
    printf("plateNumber: %d\n", cities[i].plateNumber); 
    printf("name: %s\n", cities[i].name); 
    printf("area: %lf\n", cities[i].area); 
    printf("population: %d\n", cities[i].population); 
    printf("populationDensity: %d\n", cities[i].populationDensity); 
    printf("cityCenterPopulation: %d\n", cities[i].cityCenterPopulation); 
    printf("region: %s\n", cities[i].region); 
    printf("latitude: %lf\n", cities[i].latitude); 
    printf("longitude: %lf\n", cities[i].longitude); 
} 

int main(int argc, char const *argv[]) { 
    readFile(); 
    return 0; 
} 
+0

複製整個程序是沒有用的 - 只顯示更正的行,也許一個或兩個更多的上下文。 – Caleb