本文介绍了在函数c中返回结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从函数返回struct
.看起来像这样.
I'm trying to return a struct
from a function.It looks like this..
struct read(struct returnera returnDuo, struct vara varuArray[]) {
char varunr[LISTNUMBER], varunamn[LISTNUMBER];
FILE *varuLista;
varuLista = fopen(returnDuo.filnamn, "r");
if(varuLista!=NULL) {
while(fscanf(varuLista,"%s\t%s\t%d\n",varunr, varunamn,
&varuArray[returnDuo.antalVaror].lagerSaldo) == 3){
strncpy(varuArray[returnDuo.antalVaror].varuNr,varunr,5);
strncpy(varuArray[returnDuo.antalVaror].varuNamn,varunamn,30);
returnDuo.antalVaror++;
}
printf("Filen är laddad..\n");
kommaVidare();
}
else {
printf("Filen hittades inte, skapar en tom fil"); kommaVidare();
}
fclose(varuLista);
return returnDuo;
}
我正在尝试返回returnDuo
结构中的内容,但收到错误消息:期望的标识符或'('".如果我使用void
函数,则其按预期方式工作而不返回任何内容,但我无法弄清楚如何返回此struct
.
I'm trying to return the content in the returnDuo
struct but I get the error message: "Expected identifier or '('". If I use void
function its working as expected without returning anything but I cant figure out how to return this struct
.
这是我设置结构的方式.
This is how I setup the structs.
struct vara {
char varuNr[5];
char varuNamn[50];
int lagerSaldo;
};
struct returnera {
int antalVaror;
char filnamn[LISTNUMBER];
};
以及我如何在主目录中设置它们.
And how I setup them up in main.
struct vara varuArray[SIZE];
struct returnera returnDuo = {0,"0"};
我很高兴提出了一些有关如何使其正常工作的提示...
I gladly take any tips on how to get this to work...
推荐答案
应为
struct returnera read(struct returnera returnDuo, struct vara varuArray[])
不是
struct read(struct returnera returnDuo, struct vara varuArray[])
这篇关于在函数c中返回结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!