1

I am programming in C and have initialized 2 arrays as shown below:

char names[3][10] = {"foo","ba", "foofoo"};
int values[3] = {1,2,3};

I would like to initialize these in the form:

int foo = 1;
int ba = 2;
int foofoo = 3;

This would not be a problem if the arrays were small, but I am wondering if there is a particular way in which I could initialize these variable names within a for-loop?

3
  • Nope, no can do. In c++ you could use a std::map, but even then, you would still only have 1 variable (think of it as an array) whose elements you could index with a string (foo, ba, foofoo) and whose values would contain (1,2,3). Commented Jun 22, 2014 at 8:42
  • no there is no way to do it i C but you can initialize the array within for loop Commented Jun 22, 2014 at 8:43
  • You might like to consider writing a function to be called on program start-up setting the arrays' elements as needed. Commented Jun 22, 2014 at 9:07

1 Answer 1

1

You cannot do this as C is compiled and not interpreted.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.