【问题标题】:How to reference to struct inside struct如何在结构中引用结构
【发布时间】:2014-02-24 11:15:22
【问题描述】:

我有 2 个彼此密切相关的结构,因此我希望一个结构引用另一个结构。像这样:

//inside maze.h
typedef struct{
    char * maze;
    int height, length, cols, rows;
} maze_t;

//inside walker.h
typedef struct {
    int row, col, end_row, end_col, dir, origin;
    maze_t * maze;
} walker_t;

但这是我的问题:当我想打印字符串 walker->maze->maze 时出现分段错误。这是很多代码,但我不知道我在哪里犯了错误。分段错误发生在move_walker函数中。

我的代码:

迷宫.c:

#include "maze.h"
#include "string.h"
#include "stdio.h"
#include "stdlib.h"

/* Incomplete definitions of the maze support function . */
void init_maze(maze_t* maze, FILE * pFile) {

    int result;

    // obtain file size:
    fseek(pFile , 0 , SEEK_END);
    int lSize, stringPtr;
    lSize= ftell(pFile);
    rewind (pFile);

    // allocate memory to contain the whole file:
    char* string = malloc (sizeof(lSize);
    if (string == NULL) {fputs ("Memory error",stderr); exit (2);}

    // copy the file into the buffer:
    result = fread (string,1,lSize,pFile);
    if (result != lSize) {fputs ("Reading error",stderr); exit (3);}

    fclose(pFile);

    maze->maze = malloc (strlen(string) + 1);

    stringPtr = find_letter_in_string('\n', string);
    strcpy(maze->maze, &string[stringPtr+1]);

    maze->rows = atoi(string);

    stringPtr = find_letter_in_string(',', string);
    maze->cols = atoi(&string[stringPtr+1]);

    printf("Maze has %d rows and %d columns \n", maze->rows, maze->cols);

    return;
}

walker.h:

#include "maze.h"
#include "walker.h"
#include "stdlib.h"
#include "stdio.h"


walker_t* init_walker(maze_t * maze) {

    walker_t* walker = malloc(sizeof(walker_t));

    walker->dir = 0;

    printf("Made room for walker.\n");

    walker->maze = maze;
    locate(maze, 'S',&walker->row, &walker->col);

    printf("Start coordinates: %d, %d.\n", walker->row, walker->col);

    locate(maze, 'E',&walker->end_row, &walker->end_col);

    return walker;
}

int move_walker(walker_t * walker, int row, int col) {

    printf("maze: %s", walker->maze->maze);

    printf("check: %d\n", check_move(walker->maze, row, col));
    if(! check_move(walker->maze, row, col)){
    printf("hello world");
        return 0;
    }
    walker->row = row;
    walker->col = col;
    return 1;
}

main.c:

maze = malloc( sizeof (maze_t));

FILE * pFile = fopen(argv[1],"r");
if(pFile == NULL){
    printf("No such file!\n");
    return 0;
}

init_maze(maze, pFile);
printf("Scrambled the maze.\n");


walker = init_walker(maze);
printf("Woken the walker.\n");

抱歉拼写错误等,我有阅读障碍,这不是我的母语。

【问题讨论】:

  • 你在哪里为maze_twalker_t分配内存?
  • @Arkku 在函数 init_mazeinit_walker 在我的主要函数中按此顺序排列。
  • walker->maze = maze 你必须为 walker->maze 分配内存
  • @Abhay 抱歉忘记从 main.c 添加一些代码

标签: c struct segmentation-fault


【解决方案1】:

至少这部分是错的:

result = fread (string,1,lSize,pFile);
// …
maze->maze = (char*)malloc (strlen(string) + 1);

fread 不会 NUL 终止 string,因此您不能可靠地在其上使用 strlen,因为它会寻找终止的 '\0' 并因此继续扫描您分配的缓冲区之外。在这种情况下,result 实际上包含读取的字节数,您可以使用string[result] = '\0' 终止字符串,或者直接使用fgets 读取。 strlen 本身是不必要的,因为您已经知道读取的字节数。

在任何一种情况下,您还需要为 string 中的 NUL 再分配一个字节:

char* string = malloc(lSize + 1);

也可以删除乘以 sizeof(char)(始终为 1)和转换为 char * 以获得更好的样式,如图所示。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 2012-11-25
    相关资源
    最近更新 更多