Help Manipulating String of Struct Array in Functions

This is a discussion on Help Manipulating String of Struct Array in Functions within the C Programming forums, part of the General Programming Boards category; Hello, Basically, what my code has so far is just trying to read from a file on the command line ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    9

    Help Manipulating String of Struct Array in Functions

    Hello,
    Basically, what my code has so far is just trying to read from a file on the command line and index the words of the file into an array of tableWord (which is a struct that stores the word and the line it occurs in). I get a segmentation fault on line 88 (commented). I'm attaching the entire code, however, just focus on main and wordIndex functions. My issue is probably just with understanding how memory allocation and functions work together in c because when I did this very same thing all in main I had no issues. Hope I wasn't too confusing, thanks for the help!

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    typedef struct {
        char *s;
        int line;
    } tableWord;
    
    typedef int bool;
    
    #define TRUE 1;
    #define FALSE 0;
    
    int len(char*);
    bool isEqual(char*, char*);
    FILE *openFile(int argc, char* argv[]);
    int getNumWords(FILE*, int*);
    int wordIndex(char*, tableWord*, int);
    int copyFileToString(FILE*, char*);
    
    int main(int argc, char* argv[])
    {
        char curc;
        char *s;
        int numWords, sLength=0;
        tableWord *tW;
        
        FILE *f;
        f = openFile(argc, argv);
    
        numWords = getNumWords(f, &sLength);
    
        /*Allocate memory for tableWords array*/
        if((tW = (tableWord*)malloc(sizeof(tableWord)*numWords)) == NULL)
        {
            printf("unable to allocate space for new struct");
            return 1;
        }
    
        /*Allocate memory for s*/
        if ((s=(char*)malloc(sLength+1))==NULL)
        {
            printf("unable to allocate space for s\n");
            return 1;
        }
        
        copyFileToString(f, s);
        wordIndex(s, tW, sLength);
        
        int i;
        for(i=0; i<numWords;i++)
        {
            printf("Word: %s    Line: %d\n", tW[numWords].s, tW[numWords].line);
        }
    
        return 0;
    }
    
    int wordIndex(char *s, tableWord *tW, int sLength)
    {
        int i, j, iSave, curLine = 1, wordCount=0, cCount=0;
        bool inWS = TRUE;
    
        for(i=0; i<sLength; i++)
        {
            if(inWS)        //If in white space.
                    {
                            if(s[i] > 32)   //If enter a word.
                            {
                                    iSave = i;      //Store current spot on s.
                                    inWS = FALSE;   //Now out of whitespace.
                            }
                    }
    
            else    //If not in white space.
            {
                if(s[i] < 33)    //If enter white space.
                {
                    tW[wordCount].line = curLine;
                    if((tW[wordCount].s = (char*)malloc(i-iSave+1) == NULL))
                    {
                        printf("Unable to allocate space for struct word %d", wordCount);
                        return 1;
                    }
                    for(j=iSave; j<i; j++)
                    {
                        tW[wordCount].s[cCount++] = s[j]; //Segmentation fault.
                    }
                    tW[wordCount].s[cCount] = '\0';
                    cCount = 0;
                    wordCount++;
                }
            }
    
            if(s[i] == '\n')
            {
                curLine++;
            }
    
            i++;
        }
    
        return 0;
    }
    
    int copyFileToString(FILE *f, char *s)
    {
        char curc;
        int sCount = 0;
    
        while (fscanf(f, "%c", &curc) != EOF)   //Reads next character from fil$
            {
                    s[sCount++] = curc;
            }
            s[sCount] = '\0';
    
        return 0;
    }
    
    int getNumWords(FILE *f, int *sL)
    {
        int numWords=0;
        char curc;
        bool inWS = TRUE;
    
        while(fscanf(f, "%c", &curc) != EOF)
        {
            (*sL)++;
    
            if (inWS)
                    {
                            if (curc > 33)
                            {          
                                    numWords++;
                                    inWS = FALSE;
                            }
                    }        
                    else
                    {
                            if (curc < 33)
                                    inWS = TRUE;
                    }
            }
    
            rewind(f);
        return numWords;
    }
    
    FILE *openFile(int argc, char* argv[])
    {
        if(argc == 1)
            {
                    printf("No file name specified!\n");
            }
    
        FILE *f;
            f = fopen(argv[1], "r");
        return f;
    }
    
    int len(char *s)
    {
       char *t=s;
    
       while (*t!='\0')
          t++;
       return t-s;
    }
    
    bool isEqual(char *s1, char *s2)
    {
            int i = 0;
    
            if(len(s1) != len(s2))
                    return FALSE;
    
            for(; i<len(s1); i++)
            {
                    if(s1[i] != s2[i])
                            return FALSE;
            }
    
            return TRUE;
    }

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    3
    Okay so, firstly which compiler are you using it should warn you that you are assigning something without a cast the problem isn't on line 88 its on line 81 you forgot a ).
    line 81 should look like this

    if((tW[wordCount].s = (char*)malloc(i-iSave+1)) == NULL)there you go.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    9
    sorry, i'm using gcc

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    3
    That's what i'm using hmm.. oh well. Did changing that line fix it?

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    9
    yes it did, thanks! When I ran it throug gdb it said 88 but good work figuring it out, I appreciate it.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    3
    no problem a bit of advise when using gcc use the -Wall option it will turn on all warnings and will catch things like that for you...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 17
    Last Post: 07-06-2011, 11:44 AM
  2. Manipulating Character arrays in functions.
    By kbro3 in forum C++ Programming
    Replies: 11
    Last Post: 08-16-2008, 02:24 AM
  3. Copying an array string into a struct
    By JFonseka in forum C Programming
    Replies: 2
    Last Post: 02-27-2008, 05:24 AM
  4. Manipulating Args Passed to Variadic Functions
    By CandyMan80211 in forum C Programming
    Replies: 1
    Last Post: 07-05-2007, 03:35 PM
  5. Manipulating a string
    By pabellon in forum C Programming
    Replies: 1
    Last Post: 12-19-2001, 05:19 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21