((Item*)queue_peek_head(itemQueue))->value1
The compiler needs to know the type in order to access the member of the structure.
(Also, you wouldn't do *(somePointer)->
unless there's a pointer to a pointer, there's two dereferences there.)
Question
I have a struct Item
with a variable value1
, and I placed an instance of this struct in a queue. I would now like to peek into the queue and obtain the data held in the struct instance.
The below code works as expected.
Item *itemHead = queue_peek_head(itemQueue);
printf("Head: %d\n", itemHead->value1);
However, I would like to do the same thing preferably without additional variables like itemHead
. Is there any way to do this? My original line of thinking led me to the below code which, as I figured, doesn't work (dereferencing void *
pointer, invalid void
expression).
*(queue_peek_head(itemQueue))->value1; //doesn't give me value1
Can anyone come up with a (preferably one line) way of accessing value1
from the first Item
in the queue?
Solution
((Item*)queue_peek_head(itemQueue))->value1
The compiler needs to know the type in order to access the member of the structure.
(Also, you wouldn't do *(somePointer)->
unless there's a pointer to a pointer, there's two dereferences there.)