如何将结构体数组传递给函数
我想将结构数组传递给函数,但不断收到错误。我不确定一般如何做,因为我从未被教过。我已经做了一些谷歌搜索,但我找不到任何东西。你们能帮我解释一下为什么这不起作用吗?这是整个程序的代码(很小,只是一个测试程序):
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
distance
函数完全错误。由于您有一个包含flight_time
和flight_layover
的struct
,因此您希望将这些struct
的数组传递给函数,而不是每个 int 值的数组。 IE。更改了函数签名和 for 循环内的行。
然后,在调用
distance
时,您可以将调用更改为:这将传递一个指向数组开头的指针 (
flights
) 和user_start 和
user_end
将指定用于计算距离的数组的边界。另请注意,您可能会访问
flights
数组范围之外的索引。我特别不明白为什么您有i=end+1
,也许您想要i = end - 1
?您还反转了 for 循环的条件和递减部分,我认为它应该是这样的:
编辑: 显然您的函数原型需要更新,并且您的
struct
应该在任何地方使用之前声明(包括在原型之前)。 这是经过我提到的修改的完整代码。另外,请小心使用
getNum
函数,因为它可能会在出错时返回 -1,并且如果您在数组之前访问元素,则会导致未定义的行为。您还应该检查用户输入,以确保起始值和结束值介于 1 和 5 之间,并递减,以便它们是介于 0 和 4 之间的有效数组索引。此外,您还可以使用
+= 运算符添加到变量的当前值,即
可以更改为:
EDIT2: 另一件事。按照惯例,常量
#define
应该是大写的,即。#define HOUR 60
与#define hour 60
相反。Your
distance
function is all wrong. Since you have astruct
that containsflight_time
andflight_layover
, you want to pass an array of thesestructs
to the function, not an array of each of thoseint
values. ie.Changed the function signature and the line inside the
for
loop.Then where you call
distance
, you can change the call to:That'll pass a pointer to the start of your array (
flights
), anduser_start
anduser_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 havei=end+1
, perhaps you wantedi = end - 1
?You also have the condition and decrement part of your for loop reversed, this is how I think it should be:
EDIT: Obviously your function prototype needs to be updated, and your
struct
s 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, iecan be changed to:
EDIT2: One other thing. By convention, constant
#define
s should be in upper-case, ie.#define HOUR 60
as opposed to#define hour 60
.我相信你想要这样的东西:
然后被称为
I believe you want something like this:
which is then called as