0

for simple chained (dynamic) list I want to write a function which deletes the first element which contains the char 'k'.

I´ve written the following function:

char popIfK(struct V_LISTE **kopf)
{
    char *returnVal;
    struct V_LISTE *kElement;
    kElement = *kopf;
    while (kElement->nachfolger != 'k')
    {
        kElement = kElement->nachfolger;
    }
    returnVal = &(*kopf)->vorname;
    free(*kopf);
    *kopf = kElement;
    return *returnVal;
}

In the while statement I get the warning: comparison between pointer and integer ('struct V_LISTE *' and 'int') while (kElement->nachfolger != 'k')

I have insert the elements with the function:

struct V_LISTE *insert(struct V_LISTE *list, char key)
{
    struct V_LISTE *newElement;
    newElement = (struct V_LISTE *) malloc(sizeof(struct V_LISTE));
    newElement->vorname = key;
    newElement->nachfolger = list;
    list = newElement;
    return list;
}

By calling it in main:

struct V_LISTE
{
    char vorname;
    struct V_LISTE *nachfolger;
};

int main()
{
    struct V_LISTE *kopf;
    kopf = (struct V_LISTE *) malloc(sizeof(struct V_LISTE));

    kopf->vorname = 'o';
    kopf->nachfolger = NULL;    kopf = insert(kopf, 'l');
    kopf = insert(kopf, 'k');
    kopf = insert(kopf, 'T');

    char name;
    name = popIfK(&kopf);
    printf("%c\n", name);
}

I run my program I get a segmentation fault: 11 I am not able to fi the problem and need some help please.

2
  • 3
    kElement->nachfolger should be kElement->vorname in your check for 'k'. Also, I'd suggest adding more logic to avoid crashing if your list doesn't contain a 'k'. Commented May 7, 2020 at 13:35
  • Oh - Thanks. Sometimes I should look more precisely. Commented May 7, 2020 at 13:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.