无法将值存储在库中定义的结构上

时间:2018-11-13 03:37:41

标签: 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

//它会编译,但会在控制台屏幕上显示Segmentation Fault(Core dump)。 我已经尝试了几乎所有我能想到的东西,并检查了几篇文章,但是我一直无法解决这个问题。

1 个答案:

答案 0 :(得分:0)

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