Michael B Allen wrote:
I want to initialize a static variable to a "random" value like:
static void *
get_key(struct dnsreq *req)
{
static uint16_t next_txnid = (uint32_t)req & 0xFFFF;
You can't INITIALIZE a static variable with an expression that isn't a
constant expression. But you are free to ASSIGN to a static variable
with a variable or constant expression.
do this:
static uint16_t next_txnid; /* compiler will initialize to zero */
next_txnid = (uint32_t)req & 0xFFFF; /* you assign something to it */
INITALIZATIONS are for STARTING VALUES. Since static variables START
before a program begins, the initial values for static variables must
not depend on variables (like req) whose values can't be determined
before program start. Thus, your compiler needs to know what your
static variable's value will be before program start, which obviously
can't be a variable expression. In your case, does it matter what the
value of next_txnid is at program start? Most likely no.