Reputation: 572
I'm getting "expected initializer before 'read_file' as an error. The error is on the line "instruction code[] read_file(instruction code[])." Its on line Been searching the web for help, all im finding is c++ related post, so to clarify this is for C.
I've tried moving around the positioning of the function protypes. I wrote the same program earlier that implemented linked list instead of an array and I had no errors, so I'm thinking it may have something to do with the structure array.
Thanks for the help.
#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
instruction code[501];
instruction code[] read_file(instruction code[]);
char* lookup_OP(int OP);
void print_program(instruction code[]);
void print_input_list(instruction code[]);
int main(){
code = read_file(code);
print_input_list(code);//used for debugging
print_program(code);
}
instruction code[] read_file(instruction 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);
return code;
}
Upvotes: 0
Views: 5491
Reputation: 1
Try with a function returning a calloc
-ed (see calloc(3) man page) pointer of instr
.
So
instr* read_file(const char*filename)
{
instr* arr=NULL;
int len=0, size=0;
FILE* f= fopen(filename, "r");
if (!f) { perror(filename); exit(EXIT_FAILURE); };
while (!feof (f)) {
if (len>=size-1) {
int newsize = 5*len/4+50;
instr* newarr = calloc(newsize, sizeof(instr));
if (!newarr) { perror("calloc"); exit(EXIT_FAILURE); };
if (arr) memcpy (newarr, arr, sizeof(instr)*len);
free (arr);
arr = newarr;
size = newsize;
};
if (fscanf(f, "%d %d %d",
&arr[len]->op, &arr[len]->l, &arr[len]->m)<3)
break;
len++;
}
arr[len]->op = -1; // end of array marker
fclose(f);
return arr;
}
The above function reads a heap allocated array of instr
[in pointer arr
] and is reallocating it as needed.
Don't forget to free
the result of read_file
near the end of your program.
With the above code, you can read a lot of instr
(perhaps millions on an average PC, and much more than 500). Then you'll code
int main() {
instr* code = read_file("input.txt");
print_program(code);
// at the very end of main
free(code);
}
Upvotes: 0
Reputation: 2420
This is the corrected code.
#include<stdio.h>
#include <stdlib.h>
typedef struct instruction{
int op; //opcode
int l; // L
int m; // M
} instruction;
FILE * ifp; //input file pointer
FILE * ofp; //output file pointer
instruction code[501];
instruction * read_file(instruction code[]);
char* lookup_OP(int OP);
void print_program(instruction code[]);
void print_input_list(instruction code[]);
int main(){
instruction * code = read_file(code);
print_input_list(code);//used for debugging
print_program(code);
}
instruction * read_file(instruction 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);
return code;
}
Upvotes: 0
Reputation: 225112
instruction code[] read_file(instruction code[])
is not legal syntax. You can't return an array from a function. In addition, the global code
is an array. So this assignment is illegal, too - you'll have to fix both places.
code = read_file(code);
What it looks like you want is just:
void read_file(instruction code[])
And to just call it like:
read_file(code);
No assignment necessary.
Actually now that I read some more, since code
is global, you don't need the paramters at all.
Upvotes: 1