gpt4 book ai didi

c - 不存储数据的结构指针的全局数组

转载 作者:行者123 更新时间:2023-11-30 23:56:10 24 4
gpt4 key购买 nike

我有一个试图表示房屋布局的 C 程序。它从具有以下格式的文本文件中读取房间:

RoomDoorDoor*RoomDoorDoor

The rooms and doors are stored as structures, and I have a global array of pointers to store 10 rooms. I'm using the method readrooms() to read in the rooms from the text file and store them in the array. However, after reading it in, when I try to print the contents of the array, I get a string of random characters.

#include <stdio.h>
#define MAX 10
struct room * rooms[MAX];
int rp = 0; //room count

//Declare Structures
struct room {
char *name;
struct door *doors[4];
int dp; //door count
};

struct door {
char *name;
struct room *room;
};

//Declare Functions
char *readLine(FILE *fin);
readrooms(FILE *fin);
struct door *newDoor(char * name);
struct room *newRoom(char *name);

main(int argc, char const *argv[])
{
FILE *f = fopen("C:\\Users\\s\\Documents\\C\\explore\\rooms.txt", "r");
readrooms(f);
printf("\n----- READ FILE SUCCESSFULLY | Room Count: %d -----\n", rp);

for (int i = 0; i < rp; i++) {
if (rooms[i] != NULL) {
struct room r = *rooms[i];
printf("ROOM %d: %s\n", i, r.name);
}
}


return 0;
}

struct door *newDoor(char * name) {
struct door d;

//TODO: MAKE SURE THIS IS RIGHT
d.name = name;
d.room = NULL;

return &d;
}

struct room *newRoom(char *name) {
struct room r;

r.name = name;
r.dp = 0;

rooms[rp++] = &r;

return &r;
}

char *readLine(FILE *fin) {
char *str = (char *) malloc(sizeof(char) * 3);
char current = fgetc(fin);
int iter = 0;
while (1) {
if (current == '\n') {
str[iter] = '\0';
break;
}
else if (current == EOF) return NULL;
else {
str[iter++] = current;
current = fgetc(fin);
}
}
return str;
}

readrooms(FILE *fin) {
char *curr_room = readLine(fin);

while (curr_room != NULL) {
if (strcmp(curr_room, "*") == 0) {
curr_room = readLine(fin);
continue;
}
struct room r = *newRoom(curr_room);
printf("\n\nReading room %s\n", r.name);

curr_room = readLine(fin);
while (curr_room != NULL && strcmp(curr_room, "*") != 0) {
struct door d = *newDoor(curr_room);
d.room = &r;
r.doors[r.dp++] = &d;

printf("\t%s.doors[%d] = %s\n", r.name, r.dp-1, d.name);
curr_room = readLine(fin);
//printf("Current room is now %s\n\n", curr_room);
}
}

}

这是输出:

Reading room Hall
Hall.doors[0] = Study
Hall.doors[1] = Cellar
Hall.doors[2] = Kitchen


Reading room Study
Study.doors[0] = Hall
Study.doors[1] = Garden


Reading room Cellar
Cellar.doors[0] = Hall


Reading room Kitchen
Kitchen.doors[0] = Hall
Kitchen.doors[1] = Garden


Reading room Garden
Garden.doors[0] = Study
Garden.doors[1] = Kitchen

----- READ FILE SUCCESSFULLY | Room Count: 5 -----
ROOM 0: ├ïuΣ uαΦ┤■  Y├jhxÖä
ROOM 1: É√o
ROOM 2: É√o
ROOM 3: É√o
ROOM 4: É√o

最佳答案

一个问题。

struct room *newRoom(char *name) {
struct room r;

r.name = name;
r.dp = 0;

rooms[rp++] = &r;

return &r;
}

struct room r; 是局部变量,一旦控制退出 newRoom 函数就会消失。

相反,你可以做的是

struct room *r = malloc(sizeof(struct room));
r->name = name;
r->dp = 0;

rooms[rp++] = r;

readLine 中分配足够的内存来读取完整的行,否则您最终会越界访问并调用未定义的行为。

 char *readLine(FILE *fin) {
char *str = (char *) malloc(sizeof(char) * 256);
^^^Max line length
...
}

如果你不想盲目地分配内存,realloc 就是你要找的东西。

关于c - 不存储数据的结构指针的全局数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54232406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com