1

Why does the following code display a blank linked list? I want to insert single characters to each node in the linked list. The characters are from the ch string. It works fine when I change the node member sc to integer and modify the associated code. But it seems there is an issue with using characters.

#include <stdio.h>
#include <string.h>

struct node {
    char sc[1];
    struct node *next;
};

char ch[30];

void insert(struct node **head_ref,int j) {
    struct node *new_node = (struct node*)malloc(sizeof(struct node));
    struct node *last = *head_ref;
    new_node->sc[0] = ch[j];
    new_node->next = NULL;
    if (*head_ref == NULL) {
        *head_ref=new_node;
        return;
    }
    while (last->next != NULL) {
        last = last->next;
    }
    last->next = new_node;
    return;
}

void display(struct node *head) {
    struct node *t = head;
    while (t != NULL) {
        printf("%s", t->sc);
        t = t->next;
    }
}

main() {
    FILE *fp;
    fp = fopen("C:\\Users\\Jefferson Warie\\Desktop\\input.txt", "r");
    if (fp == NULL) {
        printf("File could not be opened.");
    }
    struct node *num1h;
    num1h = NULL;
    int i = 0, j;
    char arr[100][30];
    char ch[30];
    while (!feof(fp)) {
        fgets(arr[i], 31, fp);
        i++;
    }
    for (i = 0; i < 2; i++) {
        strcpy(ch, arr[i]);
        for (j = 0; ch[j] != '\0'; j++) {
            if (i == 0) {
                insert(&num1h, j);
            }
        }
    }
    printf("First linked list: ");
    display(num1h);
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
JWX123
  • 63
  • 4

1 Answers1

0

You have declared character array ch twice — once as a global variable and once local to main.

Also, you need to add #include<stdlib.h> to use malloc

This is the final updated code.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct node {
    char sc[1];
    struct node *next;
};

char ch[30];

void insert(struct node **head_ref,int j) {
    struct node *new_node=(struct node*)malloc(sizeof(struct node));
    struct node *last=*head_ref;
    new_node->sc[0]=ch[j];
    new_node->next=NULL;
    if (*head_ref==NULL) {
        *head_ref=new_node;
        return;
    }
    while(last->next!=NULL) {
        last=last->next;
    }
    last->next=new_node;
    return;
}

void display(struct node *head) {
    struct node *t=head;
    while(t!=NULL) {
        printf("%s",t->sc);
        t=t->next;
    }
}

void main() {
    FILE *fp;
    fp=fopen("C:\\Users\\Jefferson Warie\\Desktop\\input.txt","r");
    if(fp==NULL) {
        printf("File could not be opened.");
    }
    struct node *num1h;
    num1h=NULL;
    int i=0,j;
    char arr[100][30];

    while(!feof(fp)) {
        fgets(arr[i],31,fp);
        i++;
    }
    for(i=0; i<2; i++) {
        strcpy(ch,arr[i]);
        for(j=0; ch[j]!='\0'; j++) {
            if(i==0) {
                insert(&num1h,j);
            }
        }
    }
    printf("First linked list: ");
    display(num1h);
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    Note [What should `main()` return in C and C++?](http://stackoverflow.com/questions/204476/) There is only one platform where `void main()` is approximately correct; all (hosted) platforms support `int main(void)`. Also note that [`while (!feof(file))` is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong). Your code here is no exception. You also use the wrong size in `fgets()`, and have no protection against too many lines in the file. Some of these are problems in the original too, but they should be fixed. – Jonathan Leffler Sep 17 '17 at 21:02
  • You diagnose an important aspect of the problem (the conflict between local and global variables), but you resolve it by removing the local. It would be better to counsel against the use of globals, and then arrange to pass `insert()` the character to be inserted, rather than the index into the global array of the character to be inserted. This makes the `insert()` function more useful. It also means that lines of 2000 characters (or 31 or 32 characters) could be handled cleanly, without changing the `ch` global or the `insert()` function. – Jonathan Leffler Sep 17 '17 at 21:08