不知道我是怎么得到这个bug的~>;C

不知道我是怎么得到这个bug的~>;C,c,debugging,C,Debugging,因此,我有一些代码可以逐行读取文件。 这是密码 #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct alumnus { int *yearGraduated; char firstName[30]; char lastName[30]; } Alumns; void printer ( Alumns *a ) { print

因此,我有一些代码可以逐行读取文件。
这是密码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct alumnus {

    int *yearGraduated;
    char firstName[30];
    char lastName[30];

} Alumns;

void printer ( Alumns *a ) {

    printf("Year: %*d", a->yearGraduated);
    printf("  First Name: %s", a->firstName);
    printf("  Last Name: %s", a->lastName);

}

int main(int argc, const char * argv[])
{
    Alumns a;
    char *home = getenv("HOME");
    char path[100] = "/Desktop/Alumni.txt";
    strcat(home, path);
    FILE *fp;
    fp = fopen(home, "r");

    while ( fp ) {

     fscanf(fp, "%d,%s,%s", a.yearGraduated, a.firstName, a.lastName);
     printer(&a);
    }

    return 0;

}
我的IDE对这一行做了如下说明:
格式指定类型“int”,但参数的类型为“int*”

另一个错误仅在编译代码时出现。错误位于第行:

a.yearGraduated, a.firstName, a.lastName);
我的IDE对此进行了说明:
线程1:EXC\u错误访问(代码=1,地址=0x0)

是的,文件存在,并且格式正确。
关于如何调试这个有什么想法吗?

有人建议我将结构中的int设置为非指针,但我不确定。

YearDigrade是指针。您需要为它分配空间,并在printf语句中取消引用它

所以

在功能上

 printf("Year: %d", *a->yearGraduated);

或者,您可以使结构中的int不是指针,也不会成为问题。

yearscared是指针。您需要为它分配空间,并在printf语句中取消引用它

所以

在功能上

 printf("Year: %d", *a->yearGraduated);

或者,您可以使结构中的int不是指针,也不会成为问题。

yearscared是指针。您需要为它分配空间,并在printf语句中取消引用它

所以

在功能上

 printf("Year: %d", *a->yearGraduated);

或者,您可以使结构中的int不是指针,也不会成为问题。

yearscared是指针。您需要为它分配空间,并在printf语句中取消引用它

所以

在功能上

 printf("Year: %d", *a->yearGraduated);

或者您可以使结构中的int不是指针,也不会成为问题。

关于第一个错误:您需要的所有信息都在错误消息中。提供指向
int
的指针,其中
printf
需要
int
。除非您有理由将
yearscaded
作为指针,否则应将其更改为
int

typedef struct alumnus{
    int yearGraduated;
    ...
} Alumns;
如果执行此操作,则需要将第一个参数更改为
fscanf
,以传递
a.yearscared
&a.yearscared
的地址

如果选择将其保留为指针,则必须分配:

Alumns a;
a.yearGraduated = malloc(sizeof a.yearGraduated);
然后在打印机中,必须将其解引用到
printf

printf("Year: %*d", *a->yearGraduated);
代码在
fscanf
处崩溃的原因是
a.yeargraded
处的内存未分配,因此
fscanf
尝试写入未分配的内存,这是一个坏主意

添加:(回应您的评论)


程序永远循环的原因是
while(fp)
将永远循环(除非
fopen
失败,在这种情况下它根本不会循环)。我假设您的推理是,当到达流的末尾时,
fp
将变为false。但是
fp
只是一个(指针-)值,
fscanf
不会(不能)更改它。要在到达流末尾时停止循环,请使用与第一个错误相关的:您需要的所有信息都在错误消息中。提供指向
int
的指针,其中
printf
需要
int
。除非您有理由将
yearscaded
作为指针,否则应将其更改为
int

typedef struct alumnus{
    int yearGraduated;
    ...
} Alumns;
如果执行此操作,则需要将第一个参数更改为
fscanf
,以传递
a.yearscared
&a.yearscared
的地址

如果选择将其保留为指针,则必须分配:

Alumns a;
a.yearGraduated = malloc(sizeof a.yearGraduated);
然后在打印机中,必须将其解引用到
printf

printf("Year: %*d", *a->yearGraduated);
代码在
fscanf
处崩溃的原因是
a.yeargraded
处的内存未分配,因此
fscanf
尝试写入未分配的内存,这是一个坏主意

添加:(回应您的评论)


程序永远循环的原因是
while(fp)
将永远循环(除非
fopen
失败,在这种情况下它根本不会循环)。我假设您的推理是,当到达流的末尾时,
fp
将变为false。但是
fp
只是一个(指针-)值,
fscanf
不会(不能)更改它。要在到达流末尾时停止循环,请使用与第一个错误相关的:您需要的所有信息都在错误消息中。提供指向
int
的指针,其中
printf
需要
int
。除非您有理由将
yearscaded
作为指针,否则应将其更改为
int

typedef struct alumnus{
    int yearGraduated;
    ...
} Alumns;
如果执行此操作,则需要将第一个参数更改为
fscanf
,以传递
a.yearscared
&a.yearscared
的地址

如果选择将其保留为指针,则必须分配:

Alumns a;
a.yearGraduated = malloc(sizeof a.yearGraduated);
然后在打印机中,必须将其解引用到
printf

printf("Year: %*d", *a->yearGraduated);
代码在
fscanf
处崩溃的原因是
a.yeargraded
处的内存未分配,因此
fscanf
尝试写入未分配的内存,这是一个坏主意

添加:(回应您的评论)


程序永远循环的原因是
while(fp)
将永远循环(除非
fopen
失败,在这种情况下它根本不会循环)。我假设您的推理是,当到达流的末尾时,
fp
将变为false。但是
fp
只是一个(指针-)值,
fscanf
不会(不能)更改它。要在到达流末尾时停止循环,请使用与第一个错误相关的:您需要的所有信息都在错误消息中。提供指向
int
的指针,其中
printf
需要
int
。除非您有理由将
yearscaded
作为指针,否则应将其更改为
int

typedef struct alumnus{
    int yearGraduated;
    ...
} Alumns;
如果执行此操作,则需要将第一个参数更改为
fscanf
,以传递
a.yearscared
&a.yearscared
的地址。