これを実行できるように、関数の一部を構造体ではなくintを返すように書き直しています。
if ( function(&struct1,vars) != 0 )
{
// die and give error
}
それ以外の:
struct = function(vars);
ただし、メインで構造体の値にアクセスしようとすると、関数で構造体に割り当てられた値が取得されないため、ここでいくつかの概念が欠落しています。誰かが私の記憶/変数に正確に何が起こっているのか(私はそれが起こると思うことを書きます)、そしてなぜそれが機能しないのかを私に説明していただければ幸いです。
メイン(短縮):
int main (int argc, char *argv[]) {
arguments args;
data data;
scan scans;
printf("Value at start %i\n",scans.number);
if (scan_extractor(&args,&scans) != 0) // Pass the reference of the struct to scan_extractor (while still being un-initilizaed, something I don't quite grasp).
{
printf("Error in argument reader\n");
exit(0);
} else {
printf("Main thinks %i scans\n",scans.number);
}
return(0);
}
関数(scan_extractor):
int
scan_extractor(arguments* args, scan* scans)
{
FILE* fr;
int counter = 0;
fr = fopen(args->scans,"r");
scans = calloc(MAX,sizeof(scan)); // Allocate memory to store multiple instances of the scan object
while (fgets(mzML_buffer,MAX_BUFFER,fr) != NULL)
{
(scans+counter)->id = atoi(mzML_buffer); // Modify the global version of scans
counter++;
}
scans->number = counter;
printf("Function read %i scans\n",scans->number);
return(0);
}
読み取られるファイルには、値1を含む1行が含まれています
私が得る出力は次のとおりです。
Value at start 32767
Function read 1 scans
Main thinks 32767 scans
これが私を夢中にさせる理由は、入力パラメーターを含むほぼ同一のコードのチャンクが、同様の構文を使用してmainからアクセスできることです(構造体引数のchar *値と構造体スキャンのint値のみ)。