I'm lacking understanding of pointers here. foo1 is a pointer of type foo. In dequeue, there are a list of items of type foo that are dynamically allocated passed in by queueList. My mission is to get foo1 to point to the head of the queue, however, accessing the foo1 pointer that was passed into dequeue() with *data causes a segfault. Here is where I lack understanding. Is *data not referring to the foo1 pointer?
struct foo
{
//Properties Here
};
int main()
{
struct foo* foo1;
dequeue(myQueueList, (void*) foo1);
}
void dequeue(struct queue* queueList, void **data)
{
*data = NULL; //Causes Seg fault if I do this too
*data = queueList->head->data; //Seg fault if I do this as well
return;
}
My queue is defined as:
struct queue
{
node * head;
//other stuff
};
struct node
{
void * data;
//other stuff
};
Edit: foo1 is not initialized in main. I want to initialize it in dequeue by setting it equal to a different struct foo* pointer that's been enqueued.
Edit2: queue holds a list of struct foo* pointers.
Edit3: Thanks to wildplasser, it's fixed with (void*)foo1 -> (void*)&foo1. But why do I have to use &foo1 and not (void*)?
dequeue(myQueueList, (void*) foo1);-->>dequeue(myQueueList, &foo1);Also: your *foo1 pointer is pointing nowhere; there is no foo object that it points to, and it is not NULL. - wildplasserqueueList->head->dataastruct foo*type masquerading as avoid*? - yanodatais an uninitialized pointer, so you can't dereference it. Duh. - user253751