2014-02-24 89 views
1

我有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->迷宮我得到一個分段錯誤。這是很多代碼,但我不知道我犯了什麼錯誤。分段錯誤發生在move_walker函數中。

我的代碼:

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:

#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"); 

對不起拼寫錯誤和這樣的,我有閱讀障礙旁邊的事實這不是我的母語。

+0

你在哪裏爲'maze_t'和'walker_t'分配內存? – Arkku

+0

@Arkku在函數'init_maze'和'init_walker'中依次執行我的主函數。 – Funonly

+0

_walker-> maze = maze_你必須爲walker->迷宮分配內存 –

回答

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 *乘法也可以更好的風格去除,所示。