What does "ctx" mean?

Blub picture Blub · May 25, 2011 · Viewed 20.6k times · Source

I have seen it used twice already in different libraries as an abbreviation, but I can't wrap my head around what it should mean.

For example here:

static int reformat_string(void * ctx, const unsigned char * stringVal,  
                       size_t stringLen)  
{  
    yajl_gen g = (yajl_gen) ctx;  
    return yajl_gen_status_ok == yajl_gen_string(g, stringVal, stringLen);  
}  

As far as I can tell, it is usually used for structs.

Answer

jamesdlin picture jamesdlin · May 25, 2011

It typically stands for "context". Usually this is some structure that gets passed around to functions in a library, used to maintain state (i.e., the context of the function call).

It's a preferable alternative to using global variables.