c - 无法在库中定义的结构上存储值
问题描述
主要:
#include stdio.h
#include stdlib.h
#include string.h
#include dictionary.h
int main( int argc, char ** argv ){
dictionary_t dictionary = NULL;
dictionary->entries = 1;
return 0;
}
//在标题中
#ifndef DICTIONARY_H
#define DICTIONARY_H
struct dictionary_s{
char * name;
llist_t content;
int entries;
};
typedef struct dictionary_s* dictionary_t;
#endif
//它编译但在控制台屏幕中显示分段错误(核心转储)。我已经尝试了几乎所有我能想到的并检查了几个帖子,但我一直无法解决这个问题。
解决方案
In main:
#include stdio.h
#include stdlib.h
#include string.h
#include dictionary.h
int main( int argc, char ** argv ){
//dictionary_t dictionary = NULL;//This was your old line that leads to a null pointer voilation..
dictionary_t dictionary = (dictionary_t *) malloc(sizeof(dictionary_t));
if( NULL == dictionary){
//malloc failed, what do you wanna do now?
printf("Malloc failed\n");
//exit(-1);
while(1){} //just spin forever so you can see the error i suppose?
}
dictionary->entries = 1;
return 0;
}
这是一个 malloc 示例,堆栈示例类似但不同。 https://en.wikibooks.org/wiki/C_Programming/stdlib.h/malloc
推荐阅读
- doit - Python doit 模块:如何选择运行任务操作的 shell?
- ruby-on-rails - Rails 全名方法,包含在 spans 中
- pdf - 导入和导出 PDF 和 Word 文档 SwiftUI
- javascript - 国际电话输入 JQuery 插件 - ajax html 响应后的呼叫集实例
- javascript - 在 URL 中更改查询参数时触发事件
- python - NameError:未定义名称“msg”
- swift - 如何快速更改 CVPixelBufferGetPixelFormatType
- java - 创建可能的符号时出现 Java 堆空间错误
- python - 无法在代理模式下从 FreshDesk API 获得响应 {"code" : "invalid_credentials", "message" : "您需要登录才能执行操作"}
- typo3 - TYPO3 9,10,11 语言后备似乎无法正常工作