1

I am trying to use the linux's kernel implementation of linked list(list.h), but I keep getting the following error:- invalid type argument of -> (have 'int')

struct klist
{
 int data;
 struct list_head list;
};

int main()
{
 int i;
 struct klist *_lptr;
 LIST_HEAD(klist_head);
 for(i=0;i<10;i++)
 {
  _lptr=(struct klist*)malloc(sizeof(struct klist));
  _lptr->data=i;
 }
 list_for_each_entry(_lptr,&klist_head,list)
 {
  printf("%d\n",_lptr->data);
 }
}
1
  • it would be extremely useful to annotate the line for which this error was reported.. Commented Nov 7, 2013 at 21:01

1 Answer 1

1

I have compiled and run your code, there is no error reported. (gcc 4.6.3, kubuntu 12.04)

I write one below too.

#include <stdio.h>
#include <stdlib.h>
#include "list.h"

typedef struct klist{
    int data;
    struct list_head list;
}klist;

int main()
{
    LIST_HEAD(klist_head);

    /* allocate the list */
    int i = 0;
    klist *_lptr = NULL;
    for(i = 0; i < 10; i++){
        _lptr = (klist*) malloc(sizeof(klist));
        _lptr->data = i;
        list_add(&_lptr->list, &klist_head);
    }

    /* print the list */
    list_for_each_entry(_lptr, &klist_head, list){
        printf("%d\n", _lptr->data);
    }

    /* free the list */
    struct list_head *pos = NULL, *temp = NULL;
    list_for_each_safe(pos, temp, &klist_head){
        _lptr = list_entry(pos, klist, list);
        list_del(pos);
        free(_lptr);
    }

    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

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.