如何将结构体数组传递给函数

发布于 2024-12-14 15:52:11 字数 1548 浏览 1 评论 0原文

我想将结构数组传递给函数,但不断收到错误。我不确定一般如何做,因为我从未被教过。我已经做了一些谷歌搜索,但我找不到任何东西。你们能帮我解释一下为什么这不起作用吗?这是整个程序的代码(很小,只是一个测试程序):

 #include <stdio.h>
 #include <stdlib.h>
 #define hour 60


int getNum(void);
int distance(int start, int end , int flight_time[5], int flight_layover[5]);

int main(void)
{
    int user_start=-1;
    int user_end=-1;
    int travel_time=0;

struct flight 
{
    int flight_time;
    int flight_layover;
};

struct flight flights[5]=
{
        {4 * hour + 15, 1 * hour + 20},
        {3 * hour + 58, 0 * hour + 46},
        {3 * hour + 55, 11 * hour + 29},
        {2 * hour + 14, 0 * hour + 53},
        {3 * hour + 27, 0 * hour + 0}
};

printf ("Hello sir. Please enter you starting city:\n");
user_start=getNum();
user_start--;

printf ("Good. Now enter the city you would like to end it:\n");
user_end=getNum();
user_end--;

travel_time = distance(user_start,user_end, flights[5].flight_layover,          flights[5].flight_time);

printf ("The total travel time from %d to %d is %d.",user_start, user_end, travel_time);

return 0; 
}



int distance(int start, int end , int flight_time[5], int flight_layover[5])
{
    int total_mins=0;
    int i=0;
    for (i=end+1;i--;i=start)
    {
        total_mins=total_mins + flight_time[i] + flight_layover[i];
    }

    return total_mins;
}


int getNum(void)
{
    char record[121] = {0}; 
    int number = 0;
    fgets(record,  121, stdin);
    if(sscanf_s(record, "%d", &number) != 1 )
    {
        number  = -1;
    }
    return  number;
}

I want to pass a struct array into a function and I keep getting an error. I'm not sure how to do it in general as I was never taught. I've done some googling but I can't find anything. Can you guys help me out and explain why this is not working? Here's the code of the entire program (it's small, just a test program):

 #include <stdio.h>
 #include <stdlib.h>
 #define hour 60


int getNum(void);
int distance(int start, int end , int flight_time[5], int flight_layover[5]);

int main(void)
{
    int user_start=-1;
    int user_end=-1;
    int travel_time=0;

struct flight 
{
    int flight_time;
    int flight_layover;
};

struct flight flights[5]=
{
        {4 * hour + 15, 1 * hour + 20},
        {3 * hour + 58, 0 * hour + 46},
        {3 * hour + 55, 11 * hour + 29},
        {2 * hour + 14, 0 * hour + 53},
        {3 * hour + 27, 0 * hour + 0}
};

printf ("Hello sir. Please enter you starting city:\n");
user_start=getNum();
user_start--;

printf ("Good. Now enter the city you would like to end it:\n");
user_end=getNum();
user_end--;

travel_time = distance(user_start,user_end, flights[5].flight_layover,          flights[5].flight_time);

printf ("The total travel time from %d to %d is %d.",user_start, user_end, travel_time);

return 0; 
}



int distance(int start, int end , int flight_time[5], int flight_layover[5])
{
    int total_mins=0;
    int i=0;
    for (i=end+1;i--;i=start)
    {
        total_mins=total_mins + flight_time[i] + flight_layover[i];
    }

    return total_mins;
}


int getNum(void)
{
    char record[121] = {0}; 
    int number = 0;
    fgets(record,  121, stdin);
    if(sscanf_s(record, "%d", &number) != 1 )
    {
        number  = -1;
    }
    return  number;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

假面具 2024-12-21 15:52:11

您的 distance 函数完全错误。由于您有一个包含 flight_timeflight_layoverstruct,因此您希望将这些 struct 的数组传递给函数,而不是每个 int 值的数组。 IE。

int distance(int start, int end, struct flight flights[])
{
    int total_mins=0;
    int i=0;
    for (i=end+1;i--;i=start)
    {
        total_mins=total_mins + flights[i].flight_time + flights[i].flight_layover;
    }

    return total_mins;
}

更改了函数签名和 for 循环内的行。

然后,在调用 distance 时,您可以将调用更改为:

distance(user_start,user_end, flights)

这将传递一个指向数组开头的指针 (flights) 和 user_start 和 user_end 将指定用于计算距离的数组的边界。

另请注意,您可能会访问 flights 数组范围之外的索引。我特别不明白为什么您有 i=end+1 ,也许您想要i = end - 1

您还反转了 for 循环的条件和递减部分,我认为它应该是这样的:

for (i = start; i <= end; i++)

编辑: 显然您的函数原型需要更新,并且您的 struct 应该在任何地方使用之前声明(包括在原型之前)。 这是经过我提到的修改的完整代码。

另外,请小心使用 getNum 函数,因为它可能会在出错时返回 -1,并且如果您在数组之前访问元素,则会导致未定义的行为。您还应该检查用户输入,以确保起始值和结束值介于 1 和 5 之间,并递减,以便它们是介于 0 和 4 之间的有效数组索引。

此外,您还可以使用 += 运算符添加到变量的当前值,即

total_mins=total_mins + flights[i].flight_time + flights[i].flight_layover;

可以更改为:

total_mins += flights[i].flight_time + flights[i].flight_layover;

EDIT2: 另一件事。按照惯例,常量#define应该是大写的,即。

#define HOUR 60#define hour 60 相反。

Your distance function is all wrong. Since you have a struct that contains flight_time and flight_layover, you want to pass an array of these structs to the function, not an array of each of those int values. ie.

int distance(int start, int end, struct flight flights[])
{
    int total_mins=0;
    int i=0;
    for (i=end+1;i--;i=start)
    {
        total_mins=total_mins + flights[i].flight_time + flights[i].flight_layover;
    }

    return total_mins;
}

Changed the function signature and the line inside the for loop.

Then where you call distance, you can change the call to:

distance(user_start,user_end, flights)

That'll pass a pointer to the start of your array (flights), and user_start and user_end will specify the bounds of the array to be used to calculate the distance.

Also note that you could be accessing indexes out of the flights array's bounds. I especially don't understand why you have i=end+1, perhaps you wanted i = end - 1?

You also have the condition and decrement part of your for loop reversed, this is how I think it should be:

for (i = start; i <= end; i++)

EDIT: Obviously your function prototype needs to be updated, and your structs should be declared before they're used anywhere (including before the prototypes). Here's the whole code with the modifications I've mentioned.

Also, be careful with your getNum function, since it can return -1 upon error and you'll cause undefined behaviour if you're accessing elements before your array. You should also check the user input to ensure that the start and end values are between 1 and 5, and are decremented so that they're valid array indexes between 0 and 4.

Also, you can use the += operator to add to the current value of a variable, ie

total_mins=total_mins + flights[i].flight_time + flights[i].flight_layover;

can be changed to:

total_mins += flights[i].flight_time + flights[i].flight_layover;

EDIT2: One other thing. By convention, constant #defines should be in upper-case, ie.

#define HOUR 60 as opposed to #define hour 60.

離殇 2024-12-21 15:52:11

我相信你想要这样的东西:

int distance(int start, int end , struct flight flights[5])
{
    int total_mins=0;
    int i=0;
    for (i=end+1;i--;i=start)
    {
        total_mins=total_mins + flights[i].flight_time + flights[i].flight_layover;
    }

    return total_mins;
}

然后被称为

travel_time = distance(user_start,user_end, flights);

I believe you want something like this:

int distance(int start, int end , struct flight flights[5])
{
    int total_mins=0;
    int i=0;
    for (i=end+1;i--;i=start)
    {
        total_mins=total_mins + flights[i].flight_time + flights[i].flight_layover;
    }

    return total_mins;
}

which is then called as

travel_time = distance(user_start,user_end, flights);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文