0

エラーとして「'read_file' の前にイニシャライザが必要です。エラーは "instruction code[] read_file(instruction code[])" の行にあります。そのオンライン ヘルプを求めて Web を検索していますが、見つかったのはすべて c++ です。関連する投稿なので、これを明確にするために、これは 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
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;
}
4

3 に答える 3

1

instruction code[] read_file(instruction code[])は正当な構文ではありません。関数から配列を返すことはできません。さらに、グローバルcodeは配列です。したがって、この割り当ても違法です。両方の場所を修正する必要があります。

 code = read_file(code);

あなたが望むように見えるのは、次のとおりです。

void read_file(instruction code[])

そして、次のように呼び出すだけです:

read_file(code);

割り当ては必要ありません。

実際、もう少し読んだので、codeグローバルであるため、パラメーターはまったく必要ありません。

于 2013-09-12T04:31:30.540 に答える
0

callocの-ed ( calloc(3)のマニュアル ページを参照) ポインターを返す関数を試してくださいinstr

そう

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;
}

上記の関数は、ヒープに割り当てられたinstr[in pointer arr] の配列を読み取り、必要に応じて再割り当てしています。

プログラムの終わり近くのfree結果を忘れないでください。read_file

instr上記のコードを使用すると、大量(平均的な PC ではおそらく数百万、500 をはるかに超える)を読み取ることができます。次に、コーディングします

int main() {
    instr* code = read_file("input.txt");
    print_program(code);
    // at the very end of main
    free(code);
}
于 2013-09-12T04:32:46.120 に答える
0

修正したコードです。

#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;
}
于 2013-09-12T04:37:46.743 に答える