简体   繁体   English

如何在c中通过引用传递typedef结构

[英]How to pass typedef struct by reference in c

i am trying to load data points in a txt file to a typdef struct dataset , i need a function that will split the dataset to training and testing, so it must be return two values, in c this can be done using void methods that takes params by ref, it works like this for example我正在尝试将 txt 文件中的数据点加载到typdef struct dataset ,我需要一个将数据集拆分为训练和测试的函数,因此它必须返回两个值,在 c 中这可以使用 void 方法完成参考参数,例如它的工作原理

#include<stdio.h>
void div(int a, int b, int *quotient, int *remainder) {
   *quotient = a / b;
   *remainder = a % b;
}
main() {
   int a = 76, b = 10;
   int q, r;
   div(a, b, &q, &r);
   printf("Quotient is: %d\nRemainder is: %d\n", q, r);
}

However if the param is a struct as in my case, it seems that values are not updated and remain as at the moment of initialization.但是,如果 param 是一个结构体,就像我的情况一样,值似乎没有更新,并且在初始化时保持不变。

My Question is how to make this work for struct so some thing like this will work我的问题是如何使这个对 struct 起作用,所以这样的事情会起作用

dataset *trainset, *testset;

trainset = load(filename, ratio, &testset);

The typedef struct are: typedef 结构是:

typedef struct data_member {
    double*         inputs;         /* The input data */
    double*         targets;        /* The target outputs */
} data_member;

typedef struct dataset{
    data_member*    members;        /* The members of the dataset */
    int             num_members;    /* The number of members in the set */
    int             num_inputs;     /* The number of inputs in the set */
    int             num_outputs;    /* The number of outputs in the set */
} dataset;

and the format of the file passed is和传递的文件的格式是

<Nrows> <Ninputs> <Noutputs>
<i1> <i2> <i3>.. <iN> <o1>... <oN>
<i1> <i2> <i3>.. <iN> <o1>... <oN>
<i1> <i2> <i3>.. <iN> <o1>... <oN>
<i1> <i2> <i3>.. <iN> <o1>... <oN>
..
..
<i1> <i2> <i3>.. <iN> <o1>... <oN>

Thanks谢谢

This is how you could call it:你可以这样称呼它:


typedef stuct dataset {
        struct data_member *members;
        unsigned num_members;
        unsigned num_inputs;
        unsigned num_outputs;
        } Dataset;
int load_data(Dataset *result, FILE *fp);

int main(void)
{
int rc;
FILE *fp;
Dataset the_data = {NULL, 0,0,0};

fp = fopen ("the_file", "r");
rc = load_data( &the_data, fp);

fclose (fp);
return 0;
}

int load_data(Dataset *result, FILE *fp)
{
/* ... your code here ...  */
return 13;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM