gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 04:14:48 25 4
gpt4 key购买 nike

主要内容:

#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的例子,stack的例子类似但又不同。 https://en.wikibooks.org/wiki/C_Programming/stdlib.h/malloc

关于无法将值存储在库中定义的结构上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53273426/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com