Changing variable (array of structs) from global to local (simple C program)
This is my code that I am compiling in C. Currently I have a global
variable 'code' that is an array of structs(struct instruction). I've been
trying to instead make this a local variable in main and pass it as a
parameter. Also I believe this means I will need to have read file return
a struct instruction*. I would greatly appreciate it if someone could
explain, or show me how to properly use 'code' as a local variable. Also I
am interested in what makes local variables better or more efficient than
global variables. Thanks!
#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);
}
No comments:
Post a Comment