Thread: Program Termination Problem

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    17

    Program Termination Problem

    I have posted about this program before and I thought I had it sorted but it appears I've run into trouble again. I'm makinga program that reads in a text file, reads it and enters all of the words contained within the file into a linked list which will then be seached.

    However the program appears to terminate too quickly as it never runs the printf function at the end of the makelist function.

    Any help would be grealy appreciated along with any other comments on why the program might not be working as it should.

    Code:
    typedef struct L {char *word; struct L *next;} List;
    
    int checkchar(char c) {
        if (c >= 'a' && c <= 'z') {
              return 1;
        } else if (c >= 'A' && c <= 'Z') {
             return 1;
        } else {
             return 0;
        }
    }
    
    List *insertlist(char head[], List *tail, int wordlength) {
        List *t = calloc(1, sizeof(List));
    	strncpy(t->word, head, wordlength);
    	t->next = tail;
    	printf("%s\n", head);
    	return t;
    }
    
    List *makelist(char buffer[], int filesize) {
         int i=0, j=0, count=0;
         char *word;
         List *wordlist;
         while (i <= filesize) {
               if (checkchar(buffer[i]) == 1) {
                  word[j] = buffer[i];
                  j++;
               } else {
                  wordlist = insertlist(word, wordlist, i);
                  j=0;
                  count++;
               }
               i++;
         }
         printf("%d words inserted", count);
         return wordlist;
    }
    the makelist function recieves a string of characters from the main function called "buffer".

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > strncpy(t->word, head, wordlength);
    You don't allocate any space for t->word
    Also note that strncpy doesn't always copy a \0, to make it a proper string

    > word[j] = buffer[i];
    This too is not allocated any space either.
    Nor do you append a \0 to it to make it a proper string.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    17
    so how do i allocate space? do i need to call calloc on it?

    also, is there any reason why the program doesnt run the whole way through

    thanks

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well to simplify things, start with fixed sized arrays for your words (which you know you wont exceed).

    Say
    Code:
    typedef struct L {char word[20]; struct L *next;} List;
    
    char word[20];
    > also, is there any reason why the program doesnt run the whole way through
    I just told you, you're writing to wherever your uninitialised pointers point to.
    Bad things(tm) usually happen when you do that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Multi Thread Program Problem
    By ZNez in forum C Programming
    Replies: 1
    Last Post: 01-03-2009, 11:10 AM
  3. problem w/ doubles in friend's program
    By mkylman in forum C Programming
    Replies: 16
    Last Post: 11-22-2008, 10:45 AM
  4. I have finished my program, one problem
    By sloopy in forum C Programming
    Replies: 4
    Last Post: 11-29-2005, 02:10 AM
  5. Console Program Problem
    By Breach23 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 12:35 AM