-1

我不断收到一堆错误:

  1. 参数 1 中的类型错误
  2. 重新声明之前声明的“getStudentData”
  3. “(不完整)结构 studnet”的未知字段“fname”。

任何反馈都将得到重视。谢谢

#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, &parameter-1, ...) 

        fclose(studentFile); // The function fclose will close the file. 
    }
    return 0;
}
4

2 回答 2

1
int getStudentData(struct studnet *current_ptr)
                          ^^^^^^^

你的意思是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成员,它必须是&current_ptr->gpa.

于 2013-03-17T17:33:04.283 回答
0

我的编译器推出以下输出:

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){

让我们分解这些警告和错误:

许多警告是因为您拼写错误studentstudnet. 例如,这个:

blah.c:31:30: error: incomplete definition of type 'struct studnet'
                , current_ptr->fname, current_ptr->lname, current_ptr->gpa);
                  ~~~~~~~~~~~^

我们还应该按值传递指针而不是 stuct。将一行更改为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 错误),但是如果我修复了上述错误,我会收到另一个需要修复的警告。这可以通过传递&current_ptr->gpa而不是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);
                                                          ^~~~~~~~~~~~~~~~
于 2013-03-17T17:34:43.783 回答