Reputation: 333
I have a typedef structure which defines an element in a queue. Inside the definition I have another typedef structure with its own values.
typedef struct q_elem_s
{
def_task task;
int arrive_time;
struct q_elem_s *next;
} q_elem;
Inside q_elem_s is def_task task defined by:
typedef struct task_s
{
int id;
int length;
} def_task;
When I want to access a value inside def_task task I get following error:
"invalid type argument of '->' (have 'def_task')
What am I doing wrong?
Upvotes: 0
Views: 1246
Reputation: 29794
Both ->
and .
operator are used to access structure fields. The main difference is that ->
is used when you have a pointer to a structure variable and .
is used when you have a structure variable. For example:
q_elem* q; /* q stores the address to a q_elem structure */
q_elem q; /* q stores the q_elem structure */
Now, in the first case, to access structure's members you'll have to refer them by using ->
operator.
q_elem* q;
q->task; /* access to def_task */
If you instead have a structure variable you can do:
q_elem q;
q.task; /* access to def_task */
Inside your structure q_elem
, task
is defined as a structure and not as a pointer to structure. This means that once you have access to the task
structure inside q_elem
, accessing task
's fields is achieved by .
(dot) operator.
This leave you with two choices:
q_elem* q;
q_elem->task.id
or
q_elem q;
q_elem.task.id
Notice the difference between the q_elem
variable declaration.
I hope this explain things a little.
Upvotes: 3
Reputation: 27222
If you have a q_elem
and you want to access id
from its task you'd use task.id
. Note that you don't have a def_task
pointer (def_task*
), which would need an arrow but rather a def_task
value. Try doing something like so:
q_elem q;
q.task.id = 12;
Upvotes: 2