C结构、字符串和分段错误

C结构、字符串和分段错误,c,struct,segmentation-fault,C,Struct,Segmentation Fault,所以这应该是一个协调程序,它从文本文件中获取单词。我试图使用一个struct来存储字符串,以及单词在文本文件中出现的次数。我还想将struct对象放入一个struct数组中,因为我需要在得到所有单词后按字母顺序对它们进行排序。但是,我在createStruct函数中遇到了一个分段错误。我知道问题在于我对指针和引用传递的知识有限。我已经和createStruct和compareStruct混在一起好几天了,它就是不点击 #include <stdio.h> #include <s

所以这应该是一个协调程序,它从文本文件中获取单词。我试图使用一个struct来存储字符串,以及单词在文本文件中出现的次数。我还想将struct对象放入一个struct数组中,因为我需要在得到所有单词后按字母顺序对它们进行排序。但是,我在createStruct函数中遇到了一个分段错误。我知道问题在于我对指针和引用传递的知识有限。我已经和createStruct和compareStruct混在一起好几天了,它就是不点击

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

typedef struct word{
    char *wordArr;
    int wordCount;
}word;


char *makeLowerCase(char word[]);
char *removeFirstChar(char word[]);
char *removeLastChar(char word[]);
void createStruct(struct word wordObj, char word[]);
void structCompare(struct word wordObj, struct word objArr[]);

int main(int argc, char * argv []) {

char buff[] ="@#Hello$$$$$"; //hard coded, will grab words from a .txt file
struct word newWord = {.wordArr = NULL, .wordCount = 0};
struct word structArray[500];      

makeLowerCase(buff);                 
removeFirstChar(buff);               
removeLastChar(buff);                
createStruct(newWord, buff);         
structCompare(newWord, structArray);

//trying to print from the array
printf("%s %d", structArray->wordArr, structArray->wordCount);


return 0;
}

char *makeLowerCase(char grabbedWord[]) {
    int i;
    size_t wordLength = strlen(grabbedWord);

    for(i = 0; i < wordLength; i++) {
        grabbedWord[i] = tolower(grabbedWord[i]);
    }
return grabbedWord;
};

char *removeFirstChar(char inputWord[]) {
    int i = 0;
    size_t length = strlen(inputWord);

    if (!isalnum(inputWord[i])) {
        i++;
        strncpy(inputWord, &inputWord[i], length);
        return removeFirstChar(inputWord);
    }

return inputWord;
};

char *removeLastChar(char inputWord[]) {
    size_t length = strlen(inputWord);

    if (!isalnum(inputWord[length - 1])) {
        inputWord[length - 1] = 0;
        return removeLastChar(inputWord);
    }

return inputWord;
};


void createStruct(struct word wordObj, char string[]) {
    strcpy(wordObj.wordArr, string);
    wordObj.wordCount = 1;
};

void structCompare(struct word obj, struct word structArr[]) {

    int i;

    for(i = 0; i < sizeof(structArr); i++) {

        if(structArr[i].wordCount == 0) {
        strcpy(structArr[i].wordArr, obj.wordArr);
        structArr[i].wordCount = obj.wordCount;
        }
        else if(strcmp(structArr[i].wordArr, obj.wordArr) == 0) {
            structArr->wordCount++;
        }
        else {
            strcpy(structArr[i].wordArr, obj.wordArr);
            structArr[i].wordCount = obj.wordCount;
        }
    }
};
#包括
#包括
#包括
#包括
typedef结构字{
char*wordArr;
int字数;
}字;
char*makeLowerCase(char-word[]);
char*removeFirstChar(char-word[]);
char*removeLastChar(char-word[]);
void createStruct(struct-word-obj,char-word[]);
void structCompare(struct-word-obj,struct-word-objArr[]);
int main(int argc,char*argv[]){
char buff[]=“@#Hello$$$$”//硬编码,将从.txt文件中获取单词
结构词newWord={.wordArr=NULL,.wordCount=0};
结构字结构数组[500];
makeLowerCase(buff);
移除第一个字符(浅黄色);
移除lastchar(浅黄色);
createStruct(newWord,buff);
structCompare(newWord、structArray);
//正在尝试从阵列打印
printf(“%s%d”,structary->wordArr,structary->wordCount);
返回0;
}
char*makeLowerCase(char grabbedWord[]){
int i;
字长=strlen(grabbedWord);
对于(i=0;iwordCount++;
}
否则{
strcpy(structArr[i].wordArr,obj.wordArr);
structArr[i].wordCount=obj.wordCount;
}
}
};

由于指针为NULL而导致
分段错误

要复制字符串,可以使用strcpy(char*dest,char*src)
。但是需要分配
dest
。在您的情况下,只是NULL

这就是你需要做的:

// Add a \0 to the end of a string so you know when to stop.
char buff[] ="@#Hello$$$$$\0";

// Allocate the char array so you know where to copy it. I allocate it by default to 500, change this based on your needs.
struct word newWord = {.wordArr = (char *)malloc(sizeof(char) * 500), .wordCount = 0};

如果直接将结构传递给函数,则会传递结构的副本,这样函数中所做的任何更改都不会在函数之外看到。因此,您需要将
指针
传递给
结构
,而不是实际的
结构

欢迎使用堆栈溢出!听起来您可能需要学习如何使用a来逐步完成代码。有了一个好的调试器,您可以逐行执行您的程序,并查看它偏离预期的地方。这是一个必要的工具,如果你要做任何编程。进一步阅读:。乍一看,代码中似乎存在许多问题,例如一个潜在的严重错误是
sizeof(structar)
没有按照您认为的那样执行。