0

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;
}
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
slinhart
  • 562
  • 5
  • 17
  • 1
    `instruction code[] read_file(instruction code[])` ?? – Grijesh Chauhan Sep 12 '13 at 04:30
  • 1
    You probably want `instruction *read_file(instruction code[]){ ... }`. You can't return an array from a function; you can return pointers, though. – Jonathan Leffler Sep 12 '13 at 04:31
  • Did you compile this as C or C++? It should be complaining about the use of `instruction` without an accompanying `struct` keyword. Your typdef name is `instr`, not `instruction`. Also, don't use `feof` as your loop condition; it won't return true until after you attempt to read past the end of the file, so you'll wind up executing once too often. Use the return value of `fscanf` as your loop condition instead. – John Bode Sep 12 '13 at 06:11
  • @JohnBode as a .c. And thanks for the typedef clarification, either instruction or instr worked in my code, but I suppose instr is the correct way of doing it. Thanks! – slinhart Sep 12 '13 at 16:14

3 Answers3

1

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.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • It is reasonable for a beginner, though, to assume that `[]` in the return type will mean a pointer, just like in the argument list. – Elazar Sep 12 '13 at 04:33
  • And this won't work if you read more instructions than can fit into `code` array (e.g. 500). You need to test against the dimension of `code` array. – Basile Starynkevitch Sep 12 '13 at 04:47
  • That depends on your program. A lot of people are fundamentally opposed to them, but I'm not that religious about it. – Carl Norum Sep 12 '13 at 15:08
  • @Carl Norum: If i wanted to make instruction code[501] a local variable of main and then pass it to the functions as a parameter. How would I go about doing so? I've been messing around with it and can't figure it out. (I feel like my understanding of arrays is somewhat off). Also, why is it that when i copy and paste the exact same code and resave it as a new file (in the same folder as the original), I get the error "unknown type name instruction." ?? I'm so confused – slinhart Sep 12 '13 at 15:56
  • You should probably post new questions rather than extending your question here in comments. – Carl Norum Sep 12 '13 at 16:29
0

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);
}
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Hey thanks Basile, although this code goes beyond the scope of my knowledge and contains commands that I am unfamiliar with, it is still very helpful. I will use this as a learning tool. Much appreciated – slinhart Sep 12 '13 at 15:05
  • @studmac: feel free to upvote (or accept) my answer if it fits. – Basile Starynkevitch Sep 12 '13 at 17:14
  • I wish I could! But it seems like I'm only allowed to accept one answer, and I can't upvote until I have 15rep. Once I get enough rep I'll come back and upvote tho. – slinhart Sep 12 '13 at 18:35
0

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;
}
user1969104
  • 2,340
  • 14
  • 15