设置结构指针的值

时间:2020-08-28 06:47:34

标签: c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
    char** thingSize;
} Thing;

typedef struct {
    Thing* thing;
} Game;

void load_array(Thing* thing) {
    int i, j;
    char **emptyThing = malloc(sizeof(char**));
    emptyThing = malloc(sizeof(char*)*9);
    for(i=0; i<9; i++) {
        emptyThing[i] = malloc(sizeof(char)*9);
    }
    thing -> thingSize = emptyThing;
    free(emptyThing);
}

int main(int argc, char* argv[]) {
    Game* game;
    load_array(game -> thing);
    printf("HI");
}

我遇到了细分错误,发现问题所在。 thing -> thingSize = emptyThing; 我试图将ThingSize设置为等于emptyThing的2d数组。

1 个答案:

答案 0 :(得分:2)

正如Fredrik所说,游戏指针未初始化为任何东西。它包含一个垃圾值,当取消引用它时,您将得到一个段错误。

相关问题