在函数 c 中返回结构 [英] Returning structs in function c

查看:26
本文介绍了在函数 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	%s	%d
",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..
"); 
        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];
};

以及我如何在 main 中设置它们.

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 中返回结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆