传递结构的数组的函数作为指针(C)
我试图写将初始化在数组n结构的所有值的函数。我选择使用void函数并使用结构指针。我有使用单一结构的指针没有问题,但我无法弄清楚如何将指针地址传递给结构的阵列,以我的功能。传递结构的数组的函数作为指针(C)
下面的代码产生单一错误。
typedef struct candidate {
char name[20]; //name of the election candidate
int votes; //amount of votes the candidate has
} election;
void Initialize(FILE *fp, int candidates, election *electionCandidates[]);
int main(void)
{
const int candidates = 7; //this will be the amount of structs initialized
const int voters = 365; //this will be the N iterations of a for loop for the voting process
FILE *fp = fopen ("elections.txt", "R"); //save file pointer for use when taking formatted input
election electionCandidates[candidates]; //declare 'candidates' structs, one for each candidate in the election
Initialize(fp, candidates, &electionCandidates); //save candidate names and set votes = to 0
fclose(fp);
return 0;
}
void Initialize(FILE *fp, int candidates, election *electionCandidates[]) //init values of the candidate struct array by passing pointer to void function
{
int eN = 0, N = candidates; //eN = executed number of for loop iterations, N = total number of iterations to be completed
for (eN = 0; eN < N; eN ++)
{
char name[20] = "";
fscanf (fp, "%s", &name);
strcpy(electionCandidates[eN]->name, name);
electionCandidates[eN]->votes = 0;
}
}
我有指向该行的错误:
Initialize(fp, candidates, &electionCandidates); //save candidate names and set votes = to 0
有没有人对如何解决我的语法,或者更好的办法去了解这个建议吗?
因为你有一个指向数组的指针你得到一个错误,你把它当作内部Initialize
指针数组。
在你的情况,你可以简单地传递一个简单的指针:
void Initialize(FILE *fp, int candidates, election *electionCandidates) //init values of the candidate struct array by passing pointer to void function
{
int eN = 0, N = candidates; //eN = executed number of for loop iterations, N = total number of iterations to be completed
for (eN = 0; eN < N; eN ++)
{
char name[20] = "";
fscanf (fp, "%s", &name);
strcpy(electionCandidates[eN].name, name);
electionCandidates[eN].votes = 0;
}
}
从主的通话将变为:
Initialize(fp, candidates, electionCandidates); //save candidate names and set votes = to 0
传递阵列使用指针来第一个项目在里面,这是数组名已经存在。 首先改变你的函数声明来自:
void Initialize(FILE *fp, int candidates, election *electionCandidates[]);
到
void Initialize(FILE *fp, int candidates, election *electionCandidates);
,并调用它像:
Initialize(fp, candidates, electionCandidates);
另一件事是,你访问结构项目的成员在一个数组它是指针数组。改为使用.
运算符。
这就是你应该做的,使其工作。现在我要告诉你,你搞什么名堂来完成:
- 在函数声明
election *electionCandidates[]
是一个指针类型的指针election
- 在主功能
&electionCandidates
是election
类型的指针地址
而在你的函数体electionCandidates
是一个指向数组的指针不是一个数组,这就是为什么如果你要访问数组的元素,你应该调用是这样的:
(*electionCandidates)[eN].name
感谢您的深入解释!我确实想知道我错在哪里,我认为当我设置一个指针作为参数时,我会有给它一个指针地址,当我调用函数时,而我真正需要做的是给它指向的东西。 绝对感觉就像我现在得到它! – River 2014-09-05 13:52:12
这条线:
election electionCandidates[candidates];
是election
结构类型的阵列。对于数组,你不需要明确地按引用传递就像你正在做的:
Initialize(fp, candidates, &electionCandidates);
只是这样做:
Initialize(fp, candidates, electionCandidates);
在C数组通过引用自动传递。
将'char name [20] =“”;'改为'char * name = malloc(20)'和'fscanf(fp,“%s”,&name);'''fscanf(fp,“%s”, &name)'也是一个好主意,请把它包含在你的答案中 – Igor 2014-09-05 13:36:37
谢谢!这个编译和应该工作,还没有用一个输入文件测试它看起来像我复杂它抛出所有的指针语法我记得不知道什么时候可以使用它,需要刷一下:B – River 2014-09-05 13:41:03