How does one construct and access a set of key-value pairs in C? To use a silly simple example, let\'s say I want to create a table which translates between an integer and i
There is no built-in way to do this unless you count initializing an array like this in C99:
double squareRoots[] =
{
[4] = 2.0,
[9] = 3.0,
[16] = 4.0,
[25] = 5.0,
};
However, this allocates 26 elements in the array; the other values are all zeroes.
Assuming you didn't mean this, then look at C Interfaces and Implementations by D R Hanson; it shows a way of implementing associative arrays (aka hashes or dictionaries).