数组和结构

时间:2015-04-16 00:04:15

标签: c arrays function struct

我正在参加一个介绍C的在线大学课程,我完全被我的最新项目所困扰。我的教授在网上研究错误,但这些例子似乎并不匹配。我在Visual Studio 2013上启动了该程序,现在已经从教授的指令转移到Code :: blocks。

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

typedef struct flightRec { // declare a struct to match the format of the binary data
char FlightNum[7];
char OriginAirportCode[5];
char DestAirportCode[5];
int  timestamp;
} flightRec;

int flightCmp(const void *a, const void *b) // comparison function of DestAirportCode to ascending order
{
    const flightRec *p1 = (flightRec *)a;
    const flightRec *p2 = (flightRec *)b;
    if (p1->DestAirportCode < p2->DestAirportCode)
        return -1;
    else if (p1->DestAirportCode > p2->DestAirportCode)
        return +1;
    else
        return 0;
}

int codeInst(int count, int i, char currCode, char nextCode, flightRec flightInfo[1000]) { // function that counts how many instances of each code exist
    count = 0; // set count to zero
    i = 0;
    currCode = flightInfo[i].DestAirportCode; // get a code from the array, call it currCode
    while (i < 1000 ) { // while you've not come to the end of the array
        nextCode = (&flightInfo[i+1].DestAirportCode); // get the next code from the array
        if (nextCode == currCode) { // if the next code equals the current code
            count += 1; // then add one to count
        }
        else {
            printf("Destination Airport Code: %s Count: %i", flightInfo->DestAirportCode, count); // else output code and count
            currCode = nextCode; // set currCode to nextCode
            count = 0;  // set count to 0
        }
        ++i;
    }


}

int main(){
    flightRec flightInfo[1000];
    int i = 0;
    int codeInst;

    FILE* inFile = NULL;

inFile = fopen("acars.bin", "rb");

struct tm* timestamp;

if (inFile == NULL) {
    printf("Could not open file acars.bin.\n");
    return -1;
}


while (!feof(inFile)) {
    for (i = 0; i < 1000; ++i) {
     fread(&flightInfo[i], sizeof(flightInfo), 1, inFile); // read the acars.bin file into an array of these structs
    }
    for (i = 0; i < 1000; ++i) {
        qsort(flightInfo, 1000, sizeof flightInfo, flightCmp); // sort the array
    }
    for (i = 0; i < 1000; ++i) {
        localtime(flightInfo[i].timestamp); // Do localtime () on the timestamp member of the struct, then asctime on the time struct you get back from localtime
        return timestamp;
    }
    int codeInst(flightInfo); // algorithm to count how many instances of each code exists
    for (i = 0; i < 1000; ++i) {
        printf("Flight Number: %s \nOriginal Airport: %s \nDestination Airport: %s \nDestination Airport Count:%i \nTime: %d \n\n", &flightInfo[i].FlightNum, &flightInfo[i].OriginAirportCode, &flightInfo[i].DestAirportCode, codeInst, timestamp); // print the flightRec structs in form of member columns
    }
    }
fclose(inFile);


    system("pause");
}

构建消息:

||=== Build: Debug in Project6 (compiler: GNU GCC Compiler) ===|
In function 'codeInst':|
|28|warning: assignment makes integer from pointer without a cast [enabled by default]|
|30|warning: assignment makes integer from pointer without a cast [enabled by default]|
||In function 'main':|
|70|warning: passing argument 1 of 'localtime' makes pointer from integer without a cast [enabled by default]|
|121|note: expected 'const time_t *' but argument is of type 'int'|
|71|warning: return makes integer from pointer without a cast [enabled by default]|
|74|error: expected declaration specifiers or '...' before 'flightInfo'|
|76|warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[7]' [-Wformat]|
|76|warning: format '%s' expects argument of type 'char *', but argument 3 has type 'char (*)[5]' [-Wformat]|
|76|warning: format '%s' expects argument of type 'char *', but argument 4 has type 'char (*)[5]' [-Wformat]|
|76|warning: format '%d' expects argument of type 'int', but argument 6 has type 'struct tm *' [-Wformat]|
83|warning: control reaches end of non-void function [-Wreturn-type]|
c||In function 'codeInst':|
|43|warning: control reaches end of non-void function [-Wreturn-type]|
||=== Build failed: 1 error(s), 10 warning(s) (0 minute(s), 0 second(s)) ===|

2 个答案:

答案 0 :(得分:0)

你最初有一个名为codeInst的函数,之后,在你的main函数中,你声明了一个名为codeInst的整数变量,在第73行,你编写了一个我真的不知道是什么的语句你想做(int codeInst(flightInfo);)。我假设你想要调用函数int codeInst,为此你必须:

  • 更改变量的名称,例如codeInstVarible
  • 分配给codeInstVarible函数codeInstVarible = codeInst()
  • 并填写函数中的所有现有参数,以防:(int count, int i, char currCode, char nextCode, flightRec flightInfo[1000])

答案 1 :(得分:0)

查看信息: [c function localtime()] [1]

对于错误:| 121 |注意:预期&#39; const time_t *&#39;但参数的类型为&#39; int&#39; |

在您的本地时间(flightInfo [i] .timestamp), flightInfo [i] .timestamp是一个int,而不是const time_t *(指向time_t的指针)。

对于你的int codeInst(flightInfo);函数,它应该有一个返回类型。

在你的主要功能中,你写道 int codeInst(flightInfo);

哪个不对。看看BarbarianSpock的回应。

相关问题