I don't know C.



  • We need to write something like a dictionary. I have a L type lista structure, and there's also a dictionary-- a slowka_dnia type slowo (N=25), I'm putting my vocabulary into a function init, there's a dopisz function for every element of slowka_dnia, in which function L is an index to my structure and I need to rewrite data from slowo to L.s[L.n]. And all typedef-s and funzquia init were given by the preacher.

    The problem is, in the performance of the dopisz function, that is, the malloc line is a mistake.

    typedef struct slowo {//структура с аналогом слова в одном языке и его перевод в другом
        char ang[N];
        char pol[N];
    }slowo;
    

    typedef enum sort {
    BRAK, POL_ANG, ANG_POL
    }sort;

    typedef struct lista {
    slowo** s;//1-мерный массив указатилей на структуру slowo
    int n;//кол-во элементов
    sort jak_posortowana;//как отсортирован список
    }lista;

    int init(lista* L, slowo* slowka_dnia, int n)
    {
    int i;
    for (i = 0; i<n; i++)
    if (!dopisz(L, slowka_dnia[i])) return 0;

    L-&gt;jak_posortowana = BRAK;
    L-&gt;n = n;
    return 1;
    

    }

    int dopisz(lista* L, slowo s) {
    L->s[L->n] = (slowo*)malloc(sizeof(slowo));//ошибка вот тут
    for (int i = 0; i < N; i++) L->s[L->n]->ang[i] = s.ang[i];
    for (int i = 0;i<N; i++) L->s[L->n]->pol[i] = s.pol[i];
    L->n++;
    L->jak_posortowana = BRAK;
    return 1;
    }



  • Definition lista It says lista.s It's a set of indexes, and you're in. dopisz I'm telling you what you're doing. slowo and the index received shall be assigned lista.s♪ I mean, your first mistake is that you're a simple index to assign to the index.

    The second problem is, you have a memory leak in your line.

    L->s[L->n] = (slowo*)malloc(sizeof(slowo));//ошибка вот тут 
    

    You're here for every challenge. dopisz the memory block under the structure slowo but do not do anything with the blocks that have been identified on previous challenges dopisz♪ This memory goes to the fund to support starving Africa and never comes back!

    Now function dopiszI'm out of the fact that the code above it can't be changed. ♪ dopisz You must perform three actions:

    1. Increase the number of indicators lista.s one element
    2. create a memory block for one slowo and release the indicator at the end lista.s
    3. copy the entry slowo to the newly established.

    Actions 1 and 2 are logically defined as a separate function:

    slowo ** append_slowo(slowo ** l, int new_count)
    {
        slowo ** newl = NULL;
    
    if(l &amp;&amp; new_count &gt; 1)
    {
        newl = (slowo **)realloc(l, sizeof(slowo*) * new_count);
    }
    else if(1 == new_count)
    {
        newl = (slowo **)malloc(sizeof(slowo*)); // создаем первый элемент
    }
    
    if(newl)
    {
        newl[new_count - 1] = (slowo *)malloc(sizeof(slowo));
    }
    
    return newl;        
    

    }

    Now function dopisz Adopt the following:

    int dopisz(lista* L, slowo s)
    {
    L->s = append_slowo(L->s, L->n + 1);
    if(L->s && memcpy(L->s[L->n], &s))
    {
    ++L->n;
    L->jak_posortowana = BRAK;
    return 1;
    }
    return 0;
    }




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2