[英]How to pass typedef struct by reference in c
我正在嘗試將 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);
}
但是,如果 param 是一個結構體,就像我的情況一樣,值似乎沒有更新,並且在初始化時保持不變。
我的問題是如何使這個對 struct 起作用,所以這樣的事情會起作用
dataset *trainset, *testset;
trainset = load(filename, ratio, &testset);
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;
和傳遞的文件的格式是
<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>
謝謝
你可以這樣稱呼它:
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.