简体   繁体   English

如何在结构体内部引用结构体

[英]How to reference to struct inside struct

I have 2 structs that are closely related to each other so i want one struct to reference to the other. 我有2个彼此密切相关的结构,因此我希望一个结构引用另一个。 Like so: 像这样:

//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;

But here is my problem: When i want to print the string walker->maze->maze i get a segmentation fault. 但是这是我的问题:当我想打印字符串walker-> maze-> maze时,出现了分割错误。 It's a lot of code, but i dont know where i made the mistake. 这是很多代码,但是我不知道我在哪里犯了错误。 The segmentation fault occurs in the move_walker function. 分段错误发生在move_walker函数中。

My code: 我的代码:

maze.c: maze.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: 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: 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");

Sorry for spelling mistakes and such, i have dyslexia next to the fact that this is not my native language. 很抱歉造成拼写错误,这是我不是母语,但我患有诵读困难症。

At least this part is wrong: 至少这部分是错误的:

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

fread does not NUL-terminate string , so you can't use strlen on it reliably, because it looks for the terminating '\\0' and thus continues scanning outside your allocated buffer. fread不是NUL终止的string ,因此您不能在其上可靠地使用strlen ,因为它会查找终止的'\\0'并因此继续在分配的缓冲区之外进行扫描。 result would actually contain the number of bytes read in this this case, and you could terminate the string using string[result] = '\\0' , or simply read with fgets instead. 在这种情况下, result实际上将包含读取的字节数,您可以使用string[result] = '\\0'终止字符串,或者直接使用fgets读取。 The strlen itself is unnecessary, since you already know the number of bytes read. strlen本身是不必要的,因为您已经知道读取的字节数。

In either case you also need to allocate one more byte for the NUL in string : 无论哪种情况,您都还需要为string的NUL分配一个字节:

char* string = malloc(lSize + 1);

The multiplication by sizeof(char) (always 1) and the cast to char * can also be removed for better style, as shown. 如图所示,也可以删除sizeof(char)的乘法(总是1)和强制转换为char *样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM