Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of double hash (##) in C [duplicate]

header file cissvar.h has this definition:

#define CISSQ_REQUEST_QUEUE(name, index)                                \
static __inline void                                                    \
ciss_initq_ ## name (struct ciss_softc *sc)                             \
{                                                                       \
    STAILQ_INIT(&sc->ciss_ ## name);                                    \
    CISSQ_INIT(sc, index);                                              \
}                                                                       \
(...)

And actual usage in ciss.c looks like this:

ciss_initq_free(sc);
ciss_initq_notify(sc);

It would be great if someone can explain how does this work.

So,

  1. name refers to either "free" or "notify"
  2. where does "index" come from?
  3. how does compiler do the magic binding between .h and .c here?
like image 317
hari Avatar asked Dec 21 '22 07:12

hari


1 Answers

The important lines are these (also in cissvar.h):

CISSQ_REQUEST_QUEUE(free, CISSQ_FREE);
CISSQ_REQUEST_QUEUE(notify, CISSQ_NOTIFY);

They invoke that macro that you pasted. The "##" operator concatenates two words of code together into a single word, so the code generated (with the macro expansion) for the first line looks something like this:

static __inline void                                                    
ciss_initq_free(struct ciss_softc *sc)                             
{                                                                       
    STAILQ_INIT(&sc->ciss_free);                                    
    CISSQ_INIT(sc, CISSQ_FREE);                                              
}
like image 152
DigitalGhost Avatar answered Dec 24 '22 00:12

DigitalGhost