gpt4 book ai didi

c - 为什么这部分会出现段错误?

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

这是一个不完整的代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define W 1031
#define B 256

struct FileCoordinates{
int x; /*line number*/
int y; /*word number*/
struct FileCoordinates *next;
};

struct FileStruct{
char *filename;
struct FileCoordinates *coordinates;
struct FileStruct *next;
};

struct WordStruct{
char *word;
struct WordStruct *left;
struct WordStruct *right;
struct FileStruct *files;
};

typedef struct FileCoordinates *CoorPtr;
typedef struct FileStruct *FilePtr;
typedef struct WordStruct *WordPtr;

WordPtr HashTable[W];

long int power(int a, long b){
long int pow, i;
pow = 1;
for (i = 0; i < b; i++){
pow = pow*a;
}
return pow;
}

int hashvalue (char *word){
long int i, value=0, n;
n = strlen(word);
for (i=0; i<n; i++){
value = value + power(B,n-i-1) * word[i];
}
return(value%W);
}

void putPosition(int x, int y, FilePtr *currfile){
CoorPtr currcors = (*currfile)->coordinates;
while (currcors!=NULL){
currcors = currcors->next;
}
currcors = (CoorPtr)malloc(sizeof(struct FileCoordinates));
currcors->x=x;
currcors->y=y;
}
void putFile(char *filename, WordPtr *currWord, int x, int y){
FilePtr currfile = (*currWord)->files;
while(currfile != NULL && strcmp(currfile->filename,filename)!=0){
currfile=currfile->next;
}
if (strcmp(currfile->filename,filename)==0){
putPosition(x, y, &currfile);
}
else{
currfile = (FilePtr)malloc(sizeof(struct FileStruct));
currfile->filename = filename;
putPosition(x, y, &currfile);
}
}

void insert(char *word, WordPtr *leaf, char *filename, int x, int y)
{
if( *leaf == NULL )
{
*leaf = (WordPtr) malloc( sizeof( struct WordStruct ) );
(*leaf)->word = word;
putFile(filename, &(*leaf), x, y);
/* initialize the children to null */
(*leaf)->left = 0;
(*leaf)->right = 0;
}
else if(word < (*leaf)->word)
{
insert( word, &(*leaf)->left, filename, x, y);
}
else if(word > (*leaf)->word)
{
insert( word, &(*leaf)->right, filename, x, y);
}
else if(word == (*leaf)->word){
putFile(filename, &(*leaf), x, y);
}
}

int main(int argc, char *argv[]){
int i, words, lines, value;
char *filename, *word, c;
FILE *fp;
word = (char *)malloc(21*sizeof(char));
if (argc<2){
perror("no files were inserted");
}
for (i=1; i<argc; i++){
words=1;
lines=1;
fp = fopen(argv[i], "r");
if (fp==NULL){
printf("Could not open file named %s! \n", argv[i]);
return 2;
}
filename = malloc( strlen( argv[i] ) + 1 );
strcpy( filename, argv[i] );
fscanf(fp, "%s", word);
value=hashvalue(word);
c=getc(fp);
insert(word, &HashTable[value], filename, lines, words);
if (c==' '){
words = words+1;
}
else if(c=='\n'){
lines=lines+1;
words=1;
}


}
system("PAUSE");
return 0;
}

调试器在这部分给了我段错误:

while(currfile != NULL && strcmp(currfile->filename,filename)!=0){
currfile=currfile->next;
}

代码的原因是获取文本文件作为参数,将单词排序到哈希表中的二叉树中,然后通过搜索关键字显示它出现的坐标。

无论如何,我知道这是一个非常新手的代码,但我正在努力理解。

最佳答案

创建对象时忘记将currfile->next设置为NULL:

   currfile = (FilePtr)malloc(sizeof(struct FileStruct));
currfile->filename = filename;

calloc 代替 malloc 预留空间,或者添加:

   currfile->next = NULL;

关于c - 为什么这部分会出现段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25044423/

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