Xcode 中的分段错误,调试器无法帮助我

发布于 2024-12-07 09:48:54 字数 1476 浏览 0 评论 0原文

我一直在疯狂地试图找出哪里做错了。我承认我对C缺乏经验,但我不知道哪里出了问题。我访问/使用结构的方式是否不正确?

编辑:我不断在调试器中获得 EXC_BAD_ACCESS 。

#include <stdio.h>
#include <string.h>
#define MAX_STRING 20
#define MAX_PLYR 16

typedef struct {

        char pname[MAX_STRING];
        int runs;
        char *s;

} Team_t;

int
main(void) 
{
        Team_t *team_data[MAX_PLYR];
        int i;
        char *p;
        char name[MAX_STRING];
        FILE *inp;
        inp = fopen("teamnames.rtf", "r");

        for (i = 0; i < MAX_PLYR;) {
            while ((fgets(name, MAX_STRING, inp) != NULL));
            printf("Name(i): %s\n", name);
            strcpy(team_data[i]->pname, name);
            i++;
        }
        fclose(inp);
        return(0);
}

编辑:这是更改的内容,仍然出现分段错误

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

#define MAX_STRING 20

#define MAX_PLYR 16


typedef struct {

char pname[MAX_STRING];
int runs;
char s;

} Team_t;

int
main(void) 

{
Team_t team_data[MAX_PLYR];

char name[MAX_STRING];

int i;

FILE *inp;

inp = fopen("teamnames.rtf", "r");

for (i = 0; i < MAX_PLYR; i++) {

    ((fgets(name, MAX_STRING, inp)));

    if (feof(inp)) {

        printf("End of stream\n");

        i = MAX_PLYR;

    }

    else {

        if (ferror(inp)) {

            printf("Error reading from file\n");
}

    printf("Name(i): %s\n", name);

    strcpy(team_data[i].pname, name);
}
}

fclose(inp);

return(0);
}

I have been going crazy trying to figure out what is done wrong. I admit I am inexperienced when it comes to C, but I don't know what is wrong. Is the way that I am accessing/using the struct incorrect?

EDIT: I keep getting EXC_BAD_ACCESS in debugger.

#include <stdio.h>
#include <string.h>
#define MAX_STRING 20
#define MAX_PLYR 16

typedef struct {

        char pname[MAX_STRING];
        int runs;
        char *s;

} Team_t;

int
main(void) 
{
        Team_t *team_data[MAX_PLYR];
        int i;
        char *p;
        char name[MAX_STRING];
        FILE *inp;
        inp = fopen("teamnames.rtf", "r");

        for (i = 0; i < MAX_PLYR;) {
            while ((fgets(name, MAX_STRING, inp) != NULL));
            printf("Name(i): %s\n", name);
            strcpy(team_data[i]->pname, name);
            i++;
        }
        fclose(inp);
        return(0);
}

Edit: Here's what's changed, still getting Segmentation Error

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

#define MAX_STRING 20

#define MAX_PLYR 16


typedef struct {

char pname[MAX_STRING];
int runs;
char s;

} Team_t;

int
main(void) 

{
Team_t team_data[MAX_PLYR];

char name[MAX_STRING];

int i;

FILE *inp;

inp = fopen("teamnames.rtf", "r");

for (i = 0; i < MAX_PLYR; i++) {

    ((fgets(name, MAX_STRING, inp)));

    if (feof(inp)) {

        printf("End of stream\n");

        i = MAX_PLYR;

    }

    else {

        if (ferror(inp)) {

            printf("Error reading from file\n");
}

    printf("Name(i): %s\n", name);

    strcpy(team_data[i].pname, name);
}
}

fclose(inp);

return(0);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遗心遗梦遗幸福 2024-12-14 09:48:54

您声明了 team_data 但没有分配它;因此它指向随机内存,数组的虚内容也是如此。您需要实际创建数组,例如

Team_t *team_data[MAX_PLYR] = (Team_t**) malloc(MAX_PLYR * sizeof(Team_t *));

You declare team_data but you don't allocate it; therefore it's pointing off into random memory, as are the imaginary contents of the array. You need to actually create the array, something like

Team_t *team_data[MAX_PLYR] = (Team_t**) malloc(MAX_PLYR * sizeof(Team_t *));

月牙弯弯 2024-12-14 09:48:54

使用结构,而不是指针(或者如果您坚持使用指针,请为这些结构分配空间)

Team_t team_data[MAX_PLYR];

fgets(team_data[i].pname, MAX_STRING, inp)

Use structs, not pointers (or if you insist using pointers the allocate space for those structs)

Team_t team_data[MAX_PLYR];

fgets(team_data[i].pname, MAX_STRING, inp)
卖梦商人 2024-12-14 09:48:54

当您编写时,

 Team_t *team_data[MAX_PLYR];

您没有为实际的 Team_t 记录分配任何内存,而是设置一个指向记录的指针数组。

相反,如果您写的话,

Team_t team_data[MAX_PLYR];

您就会分配记录。当您想复制到 team_data 数组时,您可以改为编写

strcpy( team_data[i].name, name );

when you write

 Team_t *team_data[MAX_PLYR];

you are not allocating any memory for the actual Team_t records, instead you are setting up an array of pointers to records.

If instead you would write

Team_t team_data[MAX_PLYR];

you would have allocated the records. When you then want to copy into the team_data array you write instead

strcpy( team_data[i].name, name );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文