2017-11-20 19 views
-1

これは、テキストファイルから単語を取得するコンコーダンスプログラムであると考えられています。私は、文字列を格納する構造体を使用しようとしていると、単語がテキストファイル内で発生回数。構造体の配列に構造体オブジェクトを配置したいと思っています。なぜなら、それらをすべて取得したらアルファベット順にソートする必要があるからです。しかし、私は、私のcreateStruct関数の中でセグメンテーションフォールトを取得しています。私は問題が指針の限られた知識と参照渡しであることを知っています。私は数日の間、createStructとcompareStructを使いこなしていました。ちょうどクリックしていません。C構造体、文字列、セグメンテーションフォールト

#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; 
     } 
    } 
}; 
+3

ようこそスタックオーバーフロー! [デバッガ](https://en.wikipedia.org/wiki/Debugger)を使用してコードをステップ実行する方法を学ぶ必要があるようです。良いデバッガを使用すると、プログラムを1行ずつ実行し、どこからずれているかを確認することができます。これはプログラミングをする場合に不可欠なツールです。さらに読む:[小さなプログラムをデバッグする方法](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) –

+1

一見すると、コードにはいくつかの問題があるようです。潜在的に重大なバグの1つは、 'sizeof(structArr)'はあなたが思っていることをしないということです。 –

答えて

0

あなたは理由NULLポインタsegmentation faultを取得します。

文字列をコピーする場合は、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}; 

あなたが直接関数に構造体を渡す場合、あなたは、任意の変更は関数内で行わので、それのcopyを通過し外部に表示されません関数のしたがって、structの代わりにpointerstructに渡す必要があります。

関連する問題