我是否因为出现了分裂错误 [英] Whya am I getting segementation fault
本文介绍了我是否因为出现了分裂错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请告诉我为什么我在此代码中出现分段错误。我在第72行遇到错误。我想通过linkA创建另一个节点链接,但我不能。当我第一次尝试访问节点时 - > linkA = temp我得到错误
我尝试过: < br $> b $ b
typedef struct node
{
char word [ 100 ];
char 含义[ 100 ];
struct node * link;
} * wordptr;
typedef struct alphanode
{
char 字[ 100 ];
char 含义[ 100 ];
struct alphanode * linkA;
struct alphanode * linkB;
} *字母;
int main()
{
int i = 1 ;
首先是字母,temp;
alphabets tempfirst =(alphabets)malloc( sizeof ( struct alphanode)) ;
tempfirst-> linkA = NULL;
tempfirst-> linkB = NULL;
first = temp;
// 创建26个链接节点
for (i = 1 ; i< = 26 ; i ++)
{
temp =(alphabets)malloc( sizeof ( struct alphanode));
tempfirst-> linkB = temp;
temp-> linkA = NULL;
temp-> linkB = NULL;
tempfirst = temp;
}
enterAlpha( bcd ,第一);
displayMyDictionary(first);
printf( Hello world!\ n);
return 0 ;
}
void enterAlpha( char str [],字母首先)
{
int i = 0 ;
// 转到alphate起始节点
的类=code-keyword>(i = 0 ; i< =(( int )str [ 0 ] - ' a'); i ++)
{
first = first-> linkB;
}
alphabets temp =(字母)malloc( sizeof ( struct alphanode));
first-> linkA = temp;
first = temp;
printf( %s,first-> word);
}
解决方案
Quote:first = temp;
应该是
first = tempfirst;
你的第一个
指针无效,因为它是用不确定的值初始化的:
/ /这些都没有初始化,因此包含不确定的值
//最好将它们设置为NULL并使用适当的检查
//以后再使用它们
alphabets first,temp;
alphabets tempfirst =(alphabets)malloc(sizeof(struct alphanode));
tempfirst-> linkA = NULL;
tempfirst-> linkB = NULL;
//因为temp没有初始化,所以首先包含一个不确定的值
first = temp;
// ...
//在这里传递无效指针
enterAlpha(bcd,first);
displayMyDictionary(first);
更好的代码会做这样的事情:
#include< assert.h>
int main()
{
alphabets first = NULL;
alphabets temp = NULL;
// ... ...
}
void enterAlpha(char str [],首先是字母)
{
//打破调试版本
断言(str!= NULL);
断言(首先!= NULL);
//使用发布版本避免seg错误
if(NULL == first)
return;
// ...
}
please tell me why am i getting segmentation fault in this code.I am getting an error at 72nd line. I wanted to make another node link though linkA but I can't. When ever I try to access the node by writing first->linkA=temp i get the error
What I have tried:
typedef struct node
{
char word[100];
char meaning[100];
struct node *link;
}*wordptr;
typedef struct alphanode
{
char word[100];
char meaning[100];
struct alphanode *linkA;
struct alphanode *linkB;
}*alphabets;
int main()
{
int i=1;
alphabets first, temp;
alphabets tempfirst=(alphabets)malloc(sizeof(struct alphanode));
tempfirst->linkA=NULL;
tempfirst->linkB=NULL;
first=temp;
//creating 26 linked nodes
for(i=1;i<=26;i++)
{
temp=(alphabets)malloc(sizeof(struct alphanode));
tempfirst->linkB=temp;
temp->linkA=NULL;
temp->linkB=NULL;
tempfirst=temp;
}
enterAlpha("bcd", first);
displayMyDictionary(first);
printf("Hello world!\n");
return 0;
}
void enterAlpha(char str[], alphabets first)
{
int i=0;
//takes to the alphate starting node
for(i=0;i<=((int)str[0]-'a');i++)
{
first=first->linkB;
}
alphabets temp = (alphabets)malloc(sizeof(struct alphanode));
first->linkA=temp;
first=temp;
printf("%s", first->word);
}
解决方案
Quote:first=temp;
should be instead
first=tempfirst;
Yourfirst
pointer is invalid because it is initialised with an indeterminate value:
// These are not initialised and contain therefore indeterminate values // It would be better to set them to NULL and use appropriate checks // when using them later alphabets first, temp; alphabets tempfirst=(alphabets)malloc(sizeof(struct alphanode)); tempfirst->linkA=NULL; tempfirst->linkB=NULL; // Because temp is not initialised, first contains an indeterminate value too first=temp; // ... // Passing invalid pointers here enterAlpha("bcd", first); displayMyDictionary(first);
Better code would do something like this:
#include <assert.h> int main() { alphabets first = NULL; alphabets temp = NULL; // ... } void enterAlpha(char str[], alphabets first) { // Break with debug builds assert(str != NULL); assert(first != NULL); // Avoid seg faults with release builds if (NULL == first) return; // ... }
这篇关于我是否因为出现了分裂错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文