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 的二维数组。
解决方案
正如 Fredrik 所说,游戏指针没有被初始化为任何东西。它持有一个垃圾值,当取消引用它时,你会得到一个段错误。
推荐阅读
- gradle - 如何将图像添加到 REST docs api 文档
- c# - 如何在不使用注释的情况下将枚举属性映射到 ServiceStack.OrmLite 中的 int?
- react-native - 在 React Native 中,Firebase 无需电子邮件即可重置密码
- javascript - 未捕获的语法错误 - 在 Javascript 中使用 PHP
- python - 在 Python 中从相同的 Concurrent.futures 多次运行函数
- javascript - 如何在不关注任何文本框的情况下使用键盘调用函数?
- c# - Azure Cosmos DB - 为查询提供分区键的方式有何不同?
- protocol-buffers - 保存解码后的 Protobuf 内容
- c# - 无法将 blob 存储文件下载到内存流
- php - 我正在使用 PHP 将数据插入 mysql DB 并且所有数据都显示为空白并且没有给我任何错误