0

Learning C, I'm trying to implement a little program. I have two structs like this:

typedef struct QueueNode_ QueueNode;
typedef struct TQueue_ TQueue;

struct QueueNode_ {
    QueueNode* next;
    const Task task;
};

struct TQueue_ {
    QueueNode* first;
    QueueNode* last;
};

Next I defined a method to initialize my queue:

void initializeQueue(TQueue* queue){
    queue = malloc(sizeof(TQueue)); //check if malloc returns NULL!
    queue->first = NULL;
    printf("%d", queue->first == NULL);
    queue->last = NULL;
}

And the main:

int main() {
    puts("TQueue example:");

    TQueue q;
    initializeQueue(&q);
    printf("%d", q.first == NULL);
}

I though that the above would print 11 but it prints 10 meaning that my pointer first is not set to null. I'm pretty sure that I miss something basic...

Why the above prints 10 and how to initialize properly the pointer of first to NULL in my initializeQueue method?

Thanks!

2 Answers 2

5

The problem is that in C, arguments are always passed by value, unless explicitly passed as a pointer, so generally speaking you should not assign the arguments directly: it is confusing.

void initializeQueue(TQueue* queue) {
    queue = malloc(sizeof(TQueue)); //check if malloc returns NULL!

See? Upon entering the function, queue points to the queue from main, but then you allocate a new struct and make queue point to it. So the queue from main is never initialized!

The solution, remove the malloc:

void initializeQueue(TQueue* queue){
    queue->first = NULL;
    printf("%d", queue->first == NULL);
    queue->last = NULL;
}

Or if you prefer to go fully dynamic, to not take the queue as an argument but return a newly allocated one:

TQueue *initializeQueue(){
    TQueue* queue = malloc(sizeof(TQueue)); //check if malloc returns NULL!
    queue->first = NULL;
    printf("%d", queue->first == NULL);
    queue->last = NULL;
    return queue;
}

And modify the main function accordingly.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes I had the first version working dynamically, but the teacher requires to use the signature void initializeQueue(TQueue* queue) and I forgot to remove the malloc call... Thanks for the answer!
3

This line is the fault:

queue = malloc(sizeof(TQueue)); //check if malloc returns NULL!

You have already allocated memory for the structure in the main function, so you don't need to do it again.

In fact, what the line is doing is allocating memory, assigning it to the pointer (and remember that arguments are passed by value so it overwrites the local copy) and you change only that structure in the function, not the one passed in by the call.

1 Comment

Arrg. Stupid of me.. Thanks :)

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.