Combining text plus numbers

I know there is a simple way to do this, but I'm having trouble figuring this out.

How do I combine some text plus a number into one variable that I can use to identify an array?

For example, I want to combine "number" plus "4" to get "number4" so I can identify an array called "number4".

You can use the C preprocessor:

#define MASHUP(text,num) text ## num

MASHUP(number,4)[5] = 0;  // Equivalent to number4[5] = 0;

--
Check out our new shield: http://www.ruggedcircuits.com/html/gadget_shield.html

I have a slight problem because I'm not necessary combining "number" and "4" together. I'm actually combining "number" and "digit1" together, where digit1 is equal to 4.

When I try the MASHUP specified above, it gives me "numberdigit1" not "number4".

Can't be done then. You can't have C evaluate a variable at runtime and use it to form another variable's name.

You're probably doing something that will require multi-dimensional arrays. Rather than referring to a sub-array by name, refer to it by a number.

--
Check out our new shield: http://www.ruggedcircuits.com/html/gadget_shield.html

Why not make a multidimensional array instead, i.e.

char number[10][10];

number[digit1][whatever] = 5;

How does this get me back the phrase "number4"?

I'm trying to find values in the correct array based on some inputs. For example, if I get 4 for digit1, then I need to pull values from an array called "number4". Surely there must be a way to concatenate these two things.

And those things are called arrays, multi-dimensional ones

Gator,

There is a way to do what you want, but it's not simple. I suspect there is an easier way to accomplish your goal.

However, if you insist on doing that, here's how it could be done:

  1. Declare your arrays.

  2. Define a structure that holds a string pointer and a pointer to an array.

  3. Declare an array of those structures, and initialize it with names and pointers.

  4. Write a function that, given a name and a number, creates a string from those two, searches the array of structures, and returns the corresponding array pointer.

For example:

int array1[10];
int array2[10];
int array3[10];

struct array_entry
    {
    char *array_name;
    int *array_ptr;
    };

struct array_entry arrays[] =
     {
     {"number1",array1},
     {"number2",array2},
     {"number3",array3}
     };

const int num_arrays = sizeof(arrays) / sizeof(struct array_entry);

The remainder is left as an exercise for the reader. :slight_smile:

Regards,

-Mike

How does this get me back the phrase "number4"?

If what you have now looks like:

char number1[][] = { { ... } };
char number2[][] = { { ... } };

you may consider replacing it with

char number[][][] = { { { ... } } };

so that your choice of number is just another dimension of the array rather than a new name of the array.

[edit]Ooops, I miss read the question at the start.

Please disregard what I said in the post

What you are looking for is sprintf().

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1279788631/#3

[/edit]

What you are looking for is sprintf().

No, I really don't think it is.

Andrew