Почему я получаю дамп ядра ошибки Segmantation в то время как exe - PullRequest
1 голос
/ 04 февраля 2020

Привет. Я пытаюсь создать программу, которая вводит слово и символ на входе и сохраняет его в массиве, создает список, в котором хранятся строки между первой и второй рекурсией символа. например, если я дам вам мир "albaniamicaoapa" и символ "a", он должен создать список, содержащий первый узел, строку "lb", второй узел - строку "mi c", третий - строку "p ». И тогда я должен напечатать их в разных строках. Я не могу понять, где я не прав, надеюсь, вы можете помочь. спасибо заранее

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 4
#define MAX_LEN 30

typedef struct node {
    char val[MAX];
    struct node * next;
} node_t;

int main (){

    char word[MAX_LEN], ch;
    int len, i, many=0,j=0;

    printf("Write a word: \n");
    fgets(word, MAX_LEN, stdin);
    printf("Write a char: \n");
    scanf("%c", &ch);

    len = strlen(word);

    node_t *head = NULL;
    head = (node_t *)malloc(sizeof(node_t));
    head->next = NULL;

    many = 2*(len-1);

    printf ("%d\n", many);

    node_t *current = head;
    node_t *test = head;
    test = (node_t *)malloc(sizeof(node_t));
    test->next = NULL;
    strcpy(test->val, "A");

    //creation nodes of the list

    for (i=0; i < len; i++){
        current->next = (node_t *)malloc(sizeof(node_t));
        current = current->next;
        current->next = NULL; 
    }

    //positioning at the head

    current = head;

    for (i=0; i < many; i++){
        if (word[i] == ch){
            i++;
            while (word[i] != ch){
                current->val[j] = word[i];
                j++;
                i++;
            }
            current = current->next;
            strcpy (current->val, "A");
            current = current->next;
            j=0;
        }
    }

    current = head;

    while (current->next != NULL) {
        if (current->val != test->val)
            printf("%s\n", current->val);
        current = current->next;
    }

    return 0;
}
...