Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
标头中函数名之前应为初始值设定项。C_C_Initializer - Fatal编程技术网

标头中函数名之前应为初始值设定项。C

标头中函数名之前应为初始值设定项。C,c,initializer,C,Initializer,我在“Read文件”之前得到“预期初始化器”作为错误。“错误”在“指令代码[]读文件(指令代码)]上。“它在网上搜索帮助,所有IM都是C++相关帖子,所以要澄清这是C. 我已经尝试过移动函数原型的位置。我之前编写的程序实现了链表而不是数组,并且没有错误,所以我认为它可能与结构数组有关 谢谢你的帮助 #include<stdio.h> #include <stdlib.h> typedef struct instruction{ int op; //opcode

我在“Read文件”之前得到“预期初始化器”作为错误。“错误”在“指令代码[]读文件(指令代码)]上。“它在网上搜索帮助,所有IM都是C++相关帖子,所以要澄清这是C.</P> 我已经尝试过移动函数原型的位置。我之前编写的程序实现了链表而不是数组,并且没有错误,所以我认为它可能与结构数组有关

谢谢你的帮助

#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;
}
#包括
#包括
typedef结构指令{
int op;//操作码
int l;//l
int m;//m
}instr;
FILE*ifp;//输入文件指针
FILE*ofp;//输出文件指针
指令代码[501];
指令代码[]读取_文件(指令代码[]);
字符*查找运算(整数运算);
无效打印程序(指令代码[]);
无效打印输入列表(说明代码[]);
int main(){
代码=读取文件(代码);
打印输入列表(代码);//用于调试
打印程序(代码);
}
指令代码[]读取文件(指令代码[]){
int i=0;
ifp=fopen(“input.txt”,“r”);
而(!feof(ifp)){
fscanf(ifp,“%d%d%d”、&code[i]->op、&code[i]->l、&code[i]->m);
i++;
}
代码[i]->op=-1;//标识数组中代码的结尾
fclose(ifp);
返回码;
}

指令代码[]读取文件(指令代码[])
不是合法语法。您不能从函数返回数组。此外,全局的
代码
是数组。因此此赋值也是非法的-您必须修复这两个位置

 code = read_file(code);
看起来你想要的只是:

void read_file(instruction code[])
就这么说吧:

read_file(code);
不需要分配任务


实际上,现在我读了更多,因为
code
是全局的,所以根本不需要参数。

尝试使用返回
instr
calloc
-ed(参见手册页)指针的函数

所以


这是正确的代码

#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;
}
#包括
#包括
typedef结构指令{
int op;//操作码
int l;//l
int m;//m
}指导;
FILE*ifp;//输入文件指针
FILE*ofp;//输出文件指针
指令代码[501];
指令*读取文件(指令代码[]);
字符*查找运算(整数运算);
无效打印程序(指令代码[]);
无效打印输入列表(说明代码[]);
int main(){
指令*代码=读取文件(代码);
打印输入列表(代码);//用于调试
打印程序(代码);
}
指令*读取文件(指令代码[]){
int i=0;
ifp=fopen(“input.txt”,“r”);
而(!feof(ifp)){
fscanf(ifp,“%d%d%d”,&code[i].op,&code[i].l,&code[i].m);
i++;
}
代码[i]。op=-1;//标识数组中代码的结尾
fclose(ifp);
返回码;
}

指令代码[]读取_文件(指令代码[])
?您可能需要
指令*读取_文件(指令代码[]){…}。不能从函数返回数组;但是,可以返回指针。是否编译为C或C++?应该抱怨使用<代码>指令<代码>,没有附带的<代码>结构> <代码>关键字。您的类型名称是“代码> IST/<代码>,而不是<代码>指令<代码>。也不要使用<代码> FEOF< <代码>您的循环条件;在您尝试读取文件末尾之前,它不会返回true,因此您将经常执行一次。请使用
fscanf
的返回值作为循环条件。@JohnBode as a.c。感谢typedef的澄清,指令或instr在我的代码中起作用,但我认为instr I这是正确的方法。谢谢!不过,对于初学者来说,假设返回类型中的
[]
表示指针是合理的,就像在参数列表中一样。如果您阅读的指令超过
code
数组中所能容纳的数量(例如500条),这将不起作用。你需要根据
code
array的维度进行测试。这取决于你的程序。很多人从根本上反对他们,但我对此并不那么虔诚。@Carl Norum:如果我想制作指令代码[501]一个main的局部变量,然后将其作为参数传递给函数。我该怎么做呢?我一直在处理它,弄不清楚它。(我觉得我对数组的理解有点不对劲)。另外,为什么我复制粘贴完全相同的代码,并将其作为新文件重新保存(在与原始代码相同的文件夹中)呢,我收到错误“未知类型名称说明”。我很困惑,您可能应该发布新问题,而不是在注释中扩展您的问题。不起作用(并且可能崩溃)如果您阅读了500多条说明。谢谢!您强化了前面答案中描述的概念。非常感谢他们Basile,虽然此代码超出了我的知识范围,包含了我不熟悉的命令,但它仍然非常有用。我将使用它作为学习工具。非常感谢appreciated@studmac:随便如果我的答案合适的话,我会投票(或接受)的。我希望我可以!但似乎我只能接受一个答案,在我有15个代表之前我不能投票。一旦我有足够的代表,我会回来投票。
int main() {
    instr* code = read_file("input.txt");
    print_program(code);
    // at the very end of main
    free(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;
}