0

I have a struct for a Double List with next and prev pointers. I'm trying to create a second list where I'm putting the fruits with odd number of calories from the first list. But I don't know why, when the second list enters in the append function, after I set the first and last pointers to NULL, only the first pointer is still set on NULL, the last pointer being uninitialised.

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<malloc.h>

typedef struct Node Node;
typedef struct DoubleList DoubleList;
typedef struct Fruit Fruit;

struct Fruit {
    char* name;
    int calories;
};

struct Node {
    Fruit info;
    Node* next, * prev;
};

struct DoubleList {
    Node* first;
    Node* last;
};

void append(DoubleList* list, Fruit f) {
    Node* new = (Node*)malloc(sizeof(Node));
    new->info = f;
    new->next = NULL;
    new->prev = NULL;

    if (list->last) {
        list->last->next = new;
        list->last = new;
    }
    else {
        list->last = list->first = new;
    }
}

void main() {
    DoubleList ld;

    ld.first = NULL;
    ld.last = NULL;

    append(&ld, createFruit("Avocado", 201));
    append(&ld, createFruit("Apple", 251));
    append(&ld, createFruit("Strawberries", 100));
    append(&ld, createFruit("Cherry", 50));

    DoubleList* ld2 = (DoubleList*)malloc(sizeof(DoubleList));
    //DoubleList ld2;
    ld2->first = NULL;
    ld2->last = NULL;
    Node* temp = ld.first;

    while (temp)
    {
        Node* aux = temp->next;
        if (temp->info.calories % 2 == 1) {
            append(&ld2, temp->info); 
            //but I don't know why this line won't send the list to the append function with the pointers set on NULL
            //append(ld2, temp->info); this line is working

        }
        else {
            free(temp->info.name);
            free(temp);
        }
        temp = aux;
    }
    show(*ld2);
}

Before entering in the append function

After entering in the append function

5
  • I would not use "new" as an identifier in a context which you tagged "C++". Commented Jun 21, 2022 at 9:10
  • Edit queue is full - Please don't tag both [C] and [C++] as your question says [C] in the title. The [C++] answer is a re-write to [C++] Commented Jun 21, 2022 at 9:10
  • ld2 is a pointer, so &ld2 creates a pointer to a pointer. Not what append expects. (Also, this is definitely C and not valid as C++). Commented Jun 21, 2022 at 9:11
  • @Yunnosch yeah, sorry, I just translated the code to English Commented Jun 21, 2022 at 9:14
  • 1
    Stop wasting time chasing bugs that the compiler has already found. Your code is invalid C, configure your compiler correctly and it will tell you as much: What compiler options are recommended for beginners learning C? Commented Jun 21, 2022 at 10:34

1 Answer 1

1

The function append is declared like:

void append(DoubleList* list, Fruit f);

That is, its first parameter has the type DoubleList *.

The variable ld2 is declared as having the same type DoubleList *:

DoubleList* ld2 = (DoubleList*)malloc(sizeof(DoubleList));

But you are calling the function append passing to the function a pointer to the pointer ld2:

append(&ld2, temp->info);

So the argument type is not compatible with the parameter type.

Also, the while loop leaves the source list ld in an indeterminate state.

Pay attention to the fact that, according to the C Standard, the function main without parameters shall be declared like:

int main( void )
Sign up to request clarification or add additional context in comments.

2 Comments

I get that, but why the first pointer save the NULL state and the last pointer is uninitialised when working with the pointer to the pointer?
@Foreastbtch In this case the function has undefined behavior.

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.