LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   access struct member error (https://www.linuxquestions.org/questions/programming-9/access-struct-member-error-438750/)

true_atlantis 04-25-2006 02:57 PM

access struct member error
 
if i have this

Code:

void ll_print(llnode **head){
    while((*head).next!=NULL)
        printf("%s\n",(*head).item);
}

where

Code:

struct llitem{
    char item[200];
    struct llitem *next;
};

typedef struct llitem llnode;

why do i get this error?

llist.c:13: error: request for member ‘next’ in something not a structure or union
llist.c:14: error: request for member ‘item’ in something not a structure or union


and how do would i go about accessing next or item?

95se 04-25-2006 03:03 PM

*head is a pointer to a structure, so you gotta use the arrow, (*head)->next

addy86 04-25-2006 03:04 PM

llnode **head
Are you sure that's what you meant (instead of llnode *head)?

By the way:
head->next
is short for
(*head).next
(and better readable).

Edit:
Code:

while((*head).next!=NULL)
        printf("%s\n",(*head).item);

will get you into an endless loop (if head->next != NULL ). You forget to move to the next node in the list.

true_atlantis 04-25-2006 03:43 PM

my problem was i needed (**head) in those two lines. i know the -> notation is better, i just have always used (*bla).

bad habbit... thanks

dmail 04-25-2006 05:00 PM

Code:

    while((*head).next!=NULL)
        printf("%s\n",(*head).item);

should be
Code:

    while((*head)->next)
    {
        printf("%s\n",(*head)->item);
        head = (*head)->next;
    }

[edit] or (**head).next

I'm bad. Got to admit it I didn't read the other posts before this. :)


All times are GMT -5. The time now is 05:09 PM.