Lemon Lemon - 2 months ago 18
C Question

C variable declaration missing

I came across the following code, I don't understand how this variable "win" works here without declaration?

//The following code is in a header file
static inline void Win_unlink(list_t * list, Win * shm_win)
{
SHM_Win_t *elem = NULL;
SHM_Win_t *tmp_elem = NULL;

SEARCH_SCALAR(*list, elem, win, shm_win);
if (elem != NULL) {
tmp_elem = elem;
MPL_DL_DELETE(*list, elem);
MPL_free(tmp_elem);
}
}

#define SEARCH_SCALAR(head,out,field,val) SEARCH_SCALAR_N(head,out,field,val,next)
#define SEARCH_SCALAR_N(head,out,field,val,_next) \
do { \
FOREACH_N(head,out,_next) { \
if ((out)->field == (val)) break; \
} \
} while(0)


I looked at the included header files, but there is no variable declared as "win". I also looked at the places where this function is called, I didn't find the declaration for "win" either. I used cscope to look for global definition of win, but I didn't see it.

Answer

Looking at the definition of SEARCH_SCALAR and SEARCH_SCALAR_N, it seems to me that win is not an independent variable. It represents a field/member of a struct.

  if ((out)->field == (val)) break;
             ^^^^ that's win in your use.