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.
kElement->nachfolgershould bekElement->vornamein your check for'k'. Also, I'd suggest adding more logic to avoid crashing if your list doesn't contain a'k'.