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?

  • 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). – enhzflep Jun 22 '14 at 08:42
  • no there is no way to do it i `C` but you can initialize the array within `for loop` – rock321987 Jun 22 '14 at 08:43
  • You might like to consider writing a function to be called on program start-up setting the arrays' elements as needed. – alk Jun 22 '14 at 09:07

1 Answers1

1

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

Ed Heal
  • 59,252
  • 17
  • 87
  • 127