これは、C でコンパイルしている私のコードです。現在、構造体 (構造体命令) の配列であるグローバル変数「コード」があります。代わりに、これをメインのローカル変数にして、パラメーターとして渡そうとしています。また、これは、ファイルを読み取って構造体命令を返す必要があることを意味すると思います*。誰かが説明してくれたり、「コード」をローカル変数として適切に使用する方法を教えてくれたりすると、とてもありがたいです。また、ローカル変数がグローバル変数よりも優れている、または効率的である理由にも興味があります。ありがとう!
#include<stdio.h>
#include <stdlib.h>
typedef struct instruction{
int op; //opcode
int l; // L
int m; // M
} instr;
FILE * ifp; //input file pointer
FILE * ofp; //output file pointer
instr code[501];
void read_file(instr code[]);
char* lookup_OP(int OP);
void print_program(instr code[]);
void print_input_list(instr code[]);
int main(){
read_file(code);
print_input_list(code);//used for debugging
print_program(code);
}
void read_file(instr code[]){
int i = 0;
ifp = fopen("input.txt", "r");
while(!feof(ifp)){
fscanf(ifp,"%d%d%d",&code[i].op, &code[i].l, &code[i].m);
i++;
}
code[i].op = -1; //identifies the end of the code in the array
fclose(ifp);
}