数组,结构,函数 [英] Arrays, Structs, Functions
问题描述
我需要一个结构。然后,让一个数组持有这些结构的7。再经过3个功能传递和转变职能内的结构数组的值,并将其传递回主。
我知道如何使一个结构。因此,让我们一起去的:
结构选{
焦炭名[20];
INT票;
};
我假设形成阵列将是:
结构大选electionCandidates [6];
为了通过一个函数。我会怎么做呢?而且是更容易返回结构数组或者我应该让它通过作为一个指针?
这可能是完全不正确的,但我总觉得人们更愿意帮助,如果他们可以看到,我其实想做这个问题并不仅仅是寻求帮助,因为我懒惰或东西。
无效初始化(选* electionCandidates []);
或者,也许这一点?
结构大选初始化(结构electionCandidates []
感谢您任何有用的意见。
无效初始化(选* electionCandidates []);
语法是错误的:你必须说结构选
结果。
在另一方面,您声明有其类型参数指针结构选阵,但你需要结构选阵。
所以,正确的版本是:
无效初始化(结构大选electionCandidates []);
由于函数的参数每个数组声明衰减,以指针,这将是等价于:
无效初始化(结构大选* electionCandidates);
下面的语法也是错误的:
结构大选初始化(结构electionCandidates []
正确的是:
结构大选初始化(结构大选electionCandidates [])
不过,这种形式不会因为你只返回1 结构
看起来顺理成章。这为什么?
参数的数组元素 electionCandidates
可以通过进行修改和恢复没有任何问题的main()
,所以一个无效
回到这里似乎是正确的。
或许,这将是最好定义的别名键入结构选
:
typedef结构大选election_t;
然后我们写:
无效初始化(election_t electrionCandidates [])
这将是非常可取的,你传递的候选人作为第二个参数的数量,因为函数没有办法Determina的数组大小:
无效初始化(election_t electrionCandidates [],INT NUM)
然后你拨打电话是这样的:
初始化(electionCandidates,6);
在这里,神奇6号是你的数组中元素的个数。
I need to make a struct. Then make an array holding 7 of these structs. Then pass it through 3 functions and change the value of the struct array within the functions and pass it back to main. I know how to make a struct. So let's go with this:
struct election{
char name[20];
int votes;
};
I'm assuming making an array would be:
struct election electionCandidates[6];
In order to pass through a function. How would I do this? And is it easier to return the struct array or should I pass it through as a pointer?
This may be completely incorrect, but I always feel like people are more willing to help if they can see that I am actually trying to do this problem and not just asking for help because I a lazy or something.
void Initialize(election *electionCandidates[]);
Or maybe this?
struct election Initialize(struct electionCandidates[]
Thank you for any helpful comments.
void Initialize(election *electionCandidates[]);
Syntax is wrong: you have to say struct election
.
On the other hand, you are declaring there a parameter having type "pointer to array of struct election", but you need "array of struct election".
So, the correct version is:
void Initialize(struct election electionCandidates[]);
Since in function arguments every array declaration "decays" to pointer, it would be equivalent to:
void Initialize(struct election *electionCandidates);
The following syntax is also wrong:
struct election Initialize(struct electionCandidates[]
The correct is:
struct election Initialize(struct election electionCandidates[])
However, this form doesn't look as logical because you are returning only 1 struct
. Which and why?
The array elements of the parameter electionCandidates
can be modified and recovered without any problem by main()
, so a void
return here seems right.
Maybe, it would be better to define an alias for the type struct election
:
typedef struct election election_t;
Then we write:
void Initialize(election_t electrionCandidates[])
It would be very desirable that you pass the number of candidates as a second parameter, since the function has no way to determina the array size:
void Initialize(election_t electrionCandidates[], int num)
Then you make the call in this way:
Initialize(electionCandidates, 6);
Here, the "magic" number 6 is the number of elements in your array.
这篇关于数组,结构,函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!