-1

So this is supposed to be a concordance program where it grabs words from a text file. I'm trying to use a struct to store the string, and also the number of times the word occurs in the text file. I also want to place the struct object into an array of structs, because I will need to sort the words alphabetically once I have them all. However, I'm getting a segmentation fault inside my createStruct function. I know the problem is with my limited knowledge of pointers and passing by reference. I've been messing with createStruct and compareStruct for days now and it just isn't clicking.

#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
    Welcome to Stack Overflow! It sounds like you may need to learn how to use a [debugger](https://en.wikipedia.org/wiki/Debugger) to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Paul R Nov 20 '17 at 08:37
  • 1
    There seem to be a number of problems in the code at first glance, e.g. one potentially serious bug is that `sizeof(structArr)` does not do what you think it does. – Paul R Nov 20 '17 at 08:37

1 Answers1

0

You get a segmentation fault because of a NULL pointer.

For copying a string, you use strcpy(char *dest, char *src). But the dest needs to be allocated. In your case, is just NULL;

So this is what you need to do:

// 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};

If you pass the struct to a function directly, you will pass a copy of it so any change done in the function, will not be seen outside of the function. So you need to pass a pointer to the struct instead of the actual struct.

Robert D. Mogos
  • 900
  • 7
  • 14