-1

I am having problems assigning ptr->str to token value. Not sure whats going on ptr->str is pointer to char so is token. The printf is not printing char value. I am getting a runtime error.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "C2A6E4_List-Driver.h"
typedef struct List
{
    struct List *next;   /* pointer to the next list node */
    char *str;           /* pointer to the string represented */
    int count;           /* # of occurrences of this string */
} List;

#endif


#define LINESIZE 256
#define DELIMETERS " "

typedef enum { false, true }bool;

List *newNode(void);
bool searchNode(List *head, char *key);

List *CreateList(FILE *fp)
{
    List *head, *ptr;
    int cnt = 0;
    size_t length;
    bool firstNode = true;
    bool srchResult = false;
    char buf[LINESIZE];

    while (fgets(buf, (int)sizeof(buf), fp) != NULL) { /* read line of file*/
        char *token = strtok(buf, DELIMETERS);
        while (token != NULL) {
            if (firstNode) {
                ptr = newNode();
                length = strlen(token) + 1;
                //  ptr->str = (char *)malloc(sizeof(char) * length);
                ptr->next = head;
                ptr->str = token;
                ptr->count = 1;
                head = ptr;
                firstNode = false;
            }
            else {
                srchResult = searchNode(head, token);
                printf("srchResult= %d\n", srchResult);
                if (srchResult) {
                    ptr = newNode();
                    length = strlen(token) + 1;
                    ptr->next = head;
                    ptr->str = token;
                    printf("ptr_str = %s", ptr->str);
                    ptr->count += 1;
                    head = ptr;
                }//end if
            }
            printf("token = %s\n", token);
            token = strtok(NULL, DELIMETERS);
        }//end while
    }// end while fgets
    return head;
}



List *newNode(void)
{
    List *ptr = (List *)malloc(sizeof(List));
    if (ptr == NULL) {
        fputs("Out of Memory\n ", stderr);
        exit(EXIT_FAILURE);
    }
    return ptr;
}


bool searchNode(List *head, char *key)
{
    List *cur = head;
    bool cmpResult = false;
    printf("key = %s\n", key);
    while (cur) {
        printf("call strmcmp\n");
        printf("cur->str = \n", cur->str);
        cmpResult = strcmp(cur->str, key);
        printf("cmpResult = %d\n", cmpResult);
        if (cmpResult == 0) {
            return false;
            cur->count += 1;
        }
        cur = cur->next;
        printf("curnext\n");
    }
    if (cmpResult > 0 || cmpResult < 0)
        cmpResult = true;
    printf("return\n");
    return cmpResult;
}


List *PrintList(const List *head)
{
    List *cur = head;
    while (cur) {
        printf(" %s  %d   ea\n", cur->str, cur->count);
        cur = cur->next;
    }
    return (List *)head;
}


void FreeList(List *head)
{
    List *cur = head, *tmp;

    while (cur) {
        tmp = head;
        head = head->next;
        free(tmp);
    }
}
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • 1
    Please [see why not to cast](http://stackoverflow.com/q/605845/2173917) the return value of `malloc()` and family in `C`. – Sourav Ghosh Aug 12 '15 at 13:49
  • Is this C or C++? There is no C/C++ language – NathanOliver Aug 12 '15 at 13:50
  • 1
    On which line do you get the "runtime error" ? Your debugger will tell you where the error occurs. What is the error message ? You don't know how to use a debugger ? Then it's the perfect moment to start learning it. – Jabberwocky Aug 12 '15 at 13:57
  • can you provide sample usage example and input file for this code ? – Zohar81 Aug 12 '15 at 13:58
  • 0) `List *head` at `CreateList` should be initialize E.g `List *head=NULL` 1) `ptr->str = token;` make clone. E.g `ptr->str = strdup(token);` – BLUEPIXY Aug 12 '15 at 14:01
  • when you do `ptr->next = head` `head` is not initialized – Ôrel Aug 12 '15 at 14:04

1 Answers1

0

the variable 'head' is not initialized in createNode, so that the following substitution of head in 'ptr->next' may result unexpected behavior when you later traverse the tree in searchNode or PrintList.

Zohar81
  • 4,554
  • 5
  • 29
  • 82