1

Why do I get this warning when assigning an anonymous function to a pointer to function field in my struct?

Here are my structs:

typedef struct list_node_t  {...} ListNode;

typedef struct list_t {
    ListNode* head;
    ListNode* current;
    ...
    //pointer to function fields
    int (*hasNext)();
    ...
}List;

And here I assign an anonymous function to hasNext. It causes the warning.

List* makeNewList( ){
    List* list = (List*)malloc(sizeof(List));
    list->head = list->tail = NULL;
    list->current = list->head;
    list->hasNext = (int   (*)(void)) (list->current==list->tail? 0:1);
    return list;
}

compiling with gcc -c -Wall list.c respond with this message:

list.c: In function ‘makeNewList’:
list.c:35:21: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
     list->hasNext = (int   (*)(void)) (list->current==list->tail? 0:1);

Why? I want list->hasNext to be a pointer to a function returning int, not a scalar int. What am I doing wrong?

Zack
  • 119
  • 2
  • 13
  • 1
    You're assigning values 0 or 1 to function pointer, not pointer to function as it should be. – koper89 Dec 08 '16 at 11:54
  • `list->hasNext = (int (*)(void)) (list->current==list->tail? 0:1);` What is this line supposed to do? It doesn't make any sense. Why would you set a function pointer to the value 1? – Lundin Dec 08 '16 at 11:54

1 Answers1

1

Why do you think that C has anonymous functions, with that particular syntax?

You need to put your foot down and actually define the function:

static int list_has_next(const List *list)
{
  return list->current == list->tail;
}

The function also (of course) has to take an argument, there are no implicit arguments in C. You need to update the function pointer in the structure to become compatible:

int (*has_next)(const List *list);

Also, stop casting the return value of malloc().

Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606
  • There's no need to keep a pointer to the same function in every node. – n. m. could be an AI Dec 08 '16 at 12:12
  • @n.m. No, of course not, but the OP seems to want one. Perhaps there are different kinds of "has next" checks depending on what goes into the list, or I don't know. – unwind Dec 08 '16 at 12:13
  • Oops. misread the code, please disregard. It's in the list, not in the list node. – n. m. could be an AI Dec 08 '16 at 12:17
  • Why do I think that C has anonymous functions? K&R shows this syntax when calling a function having pointer to function parameter: void qsort(void *lineptr, int left, int right, int (*comp)(void *, void *)); And the invocation: qsort((void**), listptr, 0, nlines-1, (int (*)(void*, void*))(numeric?numcmp:strcmp)); Oh, looking at this again, (numeric?numcmp:strcmp) return one of tow function names. there is no "inline implementation" there. – Zack Dec 08 '16 at 12:46
  • Isn't there other way to implement hasNext without sending a list as a parameter? Any other way to implement an iterator that can support hasNext and getNext without exposing the node implementation to the outside? A function that has a static list so it can remember current whenever it's called? But can have two instances of that iterator func with different list assigned to their separate static field? – Zack Dec 08 '16 at 12:53
  • @Zack This is going [XY](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), fast. :) You should probably re-post. And no, a function's static variables cannot have different values for different parallel callers. – unwind Dec 08 '16 at 12:56
  • @unwind, OK, I'll post a new question a bout implementing a List iterator and hiding the List implementation. :) Thanks. – Zack Dec 08 '16 at 13:06