Construct C code – am i missing something? [closed]-Collection of common programming errors
My compiler pushes out the following output:
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){
Let’s break down these warnings and errors:
A number of the warnings are because you mis-spelled student
as studnet
. For example, this one:
blah.c:31:30: error: incomplete definition of type 'struct studnet'
, current_ptr->fname, current_ptr->lname, current_ptr->gpa);
~~~~~~~~~~~^
We also should pass a pointer instead of a stuct by value. Changing a line to getStudentData(&str);
would help this following error:
blah.c:14:20: error: passing 'struct student' to parameter of incompatible type 'struct studnet *'
getStudentData(str);
^~~
And finally, I imagine that you wanted fscanf
and not fscan
, which would fix this error:
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"
^
There’s no warning for this following error (yet, because of the fscanf error), but if I fix the above errors, I get one more warning that needs to be fixed. This can be done by passing ¤t_ptr->gpa
instead of current_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);
^~~~~~~~~~~~~~~~
Originally posted 2013-11-09 22:49:18.