1

I have a table of variables.

int var1;
int var2;
int var3;

I would like to access values of those variables either explicitly by name, like var1 = 3; or as an array (in a for loop):

for (i = 0; i < sizeof(vars_array) / sizeof(int); i++)
{
    vars_array[i] = nnn;
}

How can I achieve this?

6
  • 2
    Can you explain the use case? Commented Jan 21, 2016 at 22:52
  • 1
    Why aree you using separate variables instead of an array in the first place? Commented Jan 21, 2016 at 23:02
  • 1
    Thanks for the replies and comments. It was a bad idea as I see it now. I'll use the array alone and create a constant definition for each array index to access in a sort of 'by name', like vars_array[VAR1] Commented Jan 21, 2016 at 23:10
  • This is what pointers are for. Commented Jan 22, 2016 at 0:07
  • In environments where they are available, this can be possible with dlopen() / dlsym(), but it's a terrible idea (other than for just funnin' around). Commented Jan 22, 2016 at 0:10

3 Answers 3

3

You cannot use:

for (i = 0; i < sizeof(vars_array); i++)
{
    vars_array[i] = nnn;
}

However, if you are willing to store an array of pointers, you can use:

int* vars_array[] = {&var1, &var2, &var3};
for (i = 0; i < sizeof(vars_array)/sizeof(vars_array[0]); i++)
{
    *vars_array[i] = nnn;
}
Sign up to request clarification or add additional context in comments.

2 Comments

You are assuming sizeof(int) == 1. This is likely incorrect.
A better technique is to end vars_array with NULL and check vars_array[i] != NULL.
1

No, you can't do this. (Note: you might be able to pull it off with some sort of horrid memory map, don't do it). You generally want to avoid dynamically referencing variables by name even if the language allows it. It makes understanding the code very difficult. What you want instead is an array or hash table to store and retrieve data in pairs.

If the variables are simply numbered var1, var2, var3... then instead of using individual variables, put the values in an array.

int vars[] = { 23, 42, 99 };
for( int i = 0; i < 3; i++ ) {
    printf("vars%d: %d\n", i, vars[i]);
}

If the variable names are not numbers, or the numbers are not contiguous, then the general idea is to use a hash table. This is like an array, but instead of using numbers of the index it uses strings. Lookup is fast, but it inherently has no order.

C doesn't have hashes built in, so you'll have to use a library like Gnome Lib.

#include <glib.h>
#include <stdio.h>

int main() {
    /* Init a hash table to take strings for keys */
    GHashTable *vars = g_hash_table_new(g_str_hash, g_str_equal);

    /* For maximum flexibility, GHashTable expects the keys and
       values to be void pointers, but you can safely store
       integers by casting them */
    g_hash_table_insert(vars, "this", (gpointer)23);
    g_hash_table_insert(vars, "that", (gpointer)42);
    g_hash_table_insert(vars, "whatever", (gpointer)99);

    /* And casting the "pointer" back to an integer */
    printf("'this' is %d\n", (int)g_hash_table_lookup(vars, "this"));

    g_hash_table_unref(vars);

    return 0;
}

Here's a good tutorial on using Gnome Lib.

1 Comment

Thanks for the info. I'll take a look at the tutorial
0

Of course you can. You would need a union:

union {
 struct {
  int var1, var2, var3;
 };
 int vars_array[3];
}name;

Although this way you should prefix your variable instances with 'name.'. You can't omit it and export the identifiers in the enclosing space (as unnamed structure and union members) for some reason.

Your code for accessing array elements (which directly map to your variables - eventually?) should look like this:

for (i = 0; i < sizeof(name.vars_array) / sizeof(int); i++)
{
    name.vars_array[i] = nnn;
}

1 Comment

OK but I decided to try a new thing you know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.