构造C代码-我遗漏了什么吗?
我不断地遇到一大堆错误:构造C代码-我遗漏了什么吗?,c,C,我不断地遇到一大堆错误: argument1中的类型错误 以前声明的“getStudentData”的重新声明 (不完整)结构studnet的未知字段“fname” 任何反馈都将被告知。谢谢 #include <stdio.h> #include <stdlib.h> struct student{ char fname[21]; char lname[21]; float gpa; } str; int getStudentData(stru
#include <stdio.h>
#include <stdlib.h>
struct student{
char fname[21];
char lname[21];
float gpa;
} str;
int getStudentData(struct studnet *current_ptr); // struct function format
int main(void){
struct student str;
getStudentData(str);
printf("Last Name: %s\n First Name: %s\n GPA: %.2f\n", str.fname, str.lname, str.gpa);
return 0;
}
int getStudentData(struct studnet *current_ptr){
FILE *studentFile; // declaring a pointer file variable
studentFile = fopen("StudnetData.txt", "r"); // format for fopen; file-variable = fopen(file_name, mode);
if ( studentFile == NULL){
printf("Error: Unable to open StudnetData.txt file\n"); //test for error
}
else {
fscan(studentFile, "%20s %20s has a GPA of %f\n"
, current_ptr->fname, current_ptr->lname, current_ptr->gpa);
// fscanf(file, format, ¶meter-1, ...)
fclose(studentFile); // The function fclose will close the file.
}
return 0;
}
#包括
#包括
结构学生{
char-fname[21];
字符名称[21];
浮动gpa;
}str;
int getStudentData(struct studnet*current_ptr);//结构函数格式
内部主(空){
struct-student-str;
获取学生数据(str);
printf(“姓氏:%s\n名字:%s\n GPA:%.2f\n”,str.fname,str.lname,str.GPA);
返回0;
}
int getStudentData(结构studnet*当前\u ptr){
FILE*studentFile;//声明指针文件变量
studentFile=fopen(“StudnetData.txt”,“r”);//fopen的格式;文件变量=fopen(文件名,模式);
如果(studentFile==NULL){
printf(“错误:无法打开StudnetData.txt文件\n”);//测试错误
}
否则{
fscan(studentFile,“%20s%20s的GPA为%f\n”
,当前搜索->fname,当前搜索->lname,当前搜索->gpa);
//fscanf(文件、格式和参数-1,…)
fclose(studentFile);//函数fclose将关闭该文件。
}
返回0;
}
你的意思是struct student
而不是struct studnet
。你不想要一个钉网,是吗
getStudentData(str);
而且,这应该是
getStudentData(&str);
由于要修改传入的结构,因此需要一个指向它的指针(在函数原型中已正确声明)
此外,您还有另一个打字错误:
fscan(studentFile, "%20s %20s has a GPA of %f\n"
函数的名称是fscanf()
,而不是fscan()
我能发现的最后一个错误:
fscanf(..., current_ptr->fname, current_ptr->lname, current_ptr->gpa)
^^^^^^^^^^^^^^^^
您没有传递指向
gpa
成员的指针,它必须是¤t\u ptr->gpa
我的编译器输出以下输出:
blah.c:10:27: warning: declaration of 'struct studnet' will not be visible outside of this function [-Wvisibility]
int getStudentData(struct studnet *current_ptr); // struct function format
^
blah.c:14:20: error: passing 'struct student' to parameter of incompatible type 'struct studnet *'
getStudentData(str);
^~~
blah.c:10:36: note: passing argument to parameter 'current_ptr' here
int getStudentData(struct studnet *current_ptr); // struct function format
^
blah.c:20:27: warning: declaration of 'struct studnet' will not be visible outside of this function [-Wvisibility]
int getStudentData(struct studnet *current_ptr){
^
blah.c:20:5: error: conflicting types for 'getStudentData'
int getStudentData(struct studnet *current_ptr){
^
blah.c:10:5: note: previous declaration is here
int getStudentData(struct studnet *current_ptr); // struct function format
^
blah.c:30:9: warning: implicit declaration of function 'fscan' is invalid in C99 [-Wimplicit-function-declaration]
fscan(studentFile, "%20s %20s has a GPA of %f\n"
^
blah.c:31:30: error: incomplete definition of type 'struct studnet'
, current_ptr->fname, current_ptr->lname, current_ptr->gpa);
~~~~~~~~~~~^
blah.c:20:27: note: forward declaration of 'struct studnet'
int getStudentData(struct studnet *current_ptr){
blah.c:31:59: warning: format specifies type 'float *' but the argument has type 'double' [-Wformat]
, current_ptr->fname, current_ptr->lname, current_ptr->gpa);
^~~~~~~~~~~~~~~~
让我们分析一下这些警告和错误:
许多警告是因为您将student
拼写错误为studnet
。例如,这个:
blah.c:31:30: error: incomplete definition of type 'struct studnet'
, current_ptr->fname, current_ptr->lname, current_ptr->gpa);
~~~~~~~~~~~^
我们还应该传递指针,而不是stuct by值。将行更改为getStudentData(&str)代码>将有助于解决以下错误:
blah.c:14:20: error: passing 'struct student' to parameter of incompatible type 'struct studnet *'
getStudentData(str);
^~~
最后,我假设您想要的是fscanf
,而不是fscan
,这将修复此错误:
blah.c:30:9: warning: implicit declaration of function 'fscan' is invalid in C99 [-Wimplicit-function-declaration]
fscan(studentFile, "%20s %20s has a GPA of %f\n"
^
下面的错误没有警告(由于fscanf错误,现在还没有),但是如果我修复了上面的错误,我会得到另外一个需要修复的警告。这可以通过传递¤t\u ptr->gpa
而不是current\u ptr->gpa
来完成
blah.c:31:59: warning: format specifies type 'float *' but the argument has type 'double' [-Wformat]
, current_ptr->fname, current_ptr->lname, current_ptr->gpa);
^~~~~~~~~~~~~~~~
您输入了student
:struct studnet
您必须调用getStudentData
,地址为str
(使用&str
),因为它需要指向struct student
的指针作为参数。作为提示,在发布错误问题时,请始终包含实际错误,全部未经编辑。同时指出错误在代码中的位置。我感谢代码中的所有输入。下次,我应该更加警惕!