[英]Can't store values on a struct defined in a library
In main: 在主要方面:
#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;
}
//In header //在标题中
#ifndef DICTIONARY_H
#define DICTIONARY_H
struct dictionary_s{
char * name;
llist_t content;
int entries;
};
typedef struct dictionary_s* dictionary_t;
#endif
//It compiles but shows Segmentation Fault (Core dump) in the console screen. //虽然可以编译,但是会在控制台屏幕上显示Segmentation Fault(Core dump)。 I have tried almost everything I can think of and checked several posts, but I've been unable to solve this problem.
我已经尝试了几乎所有我能想到的东西,并检查了几篇文章,但是我一直无法解决这个问题。
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;
}
Here is a malloc example, the stack example is similar but different. 这是一个malloc示例,堆栈示例相似但不同。 https://en.wikibooks.org/wiki/C_Programming/stdlib.h/malloc
https://zh.wikibooks.org/wiki/C_Programming/stdlib.h/malloc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.