I have a structure that only one function must access. The function converts tokens like "k, K, kb, KB, m, M, mb, MB, ..." into an actual unit. The purpose of this is to simplify a configuration file.
So, suppose we have:
static uint32_t real_unit(const char *str)
{
struct u2type {
char key[3];
uint32_t val;
} const u2types[] = {
{ "k", KB_UNIT },
{ "K", KB_UNIT },
{ "kb", KB_UNIT },
{ "KB", KB_UNIT },
{ "m", MB_UNIT },
{ "M", MB_UNIT },
{ "mb", MB_UNIT },
{ "MB", MB_UNIT },
{ "g", GB_UNIT },
{ "G", GB_UNIT },
{ "gb", GB_UNIT },
{ "GB", GB_UNIT },
{ { 0 }, 0 }
};
... code to look up str ...
}
I have seen other programs where struct u2type would be declared as static (again, within a function) and I can't see how that is useful. The structure is not going to change, it will always be the same every time the function is entered. That's why I made it const.
Yet, I have seen many people do statc struct foo { ... } const foos[] = { ... }, within a function where the scope is just obvious.
Is there any benefit to doing that? I try to study ASM output prior to coming to SO with optimization questions like these, but I am not an assembly guru :)
EDIT:
Yes, I know this approach smells like feet. Some projects just have odd requirements (typically mandated by odd people). The question, however remains entirely separate from the use of the function.