# Thread: copy contents of array to single value?

1. ## copy contents of array to single value?

I have an array, lets call it buffer.

buffer[0] = 9
buffer[1] = 2
buffer[2] = 3

how would i get a variable, x, to equal 923 (not only those numbers, but the contents of the buffer, whatever they may be)

2. Well you could start from the back of the array and as you go backwards through it summing, just create a multiple that gets 10 times bigger everytime you decrement the index.

Code:
int x;
int sum = 0;
int multiple = 1;
for (x = 2; x > 0; x--) {
sum += buffer[x] * multiple;
mutiple *= 10;
}
Another way would be to use the itoa() function on each one and strcat() the values together and atoi() that value.

Either way, I'm sure there is a better way. I wouldn't be suprised if one of the wiseasses in here came in with a STL function that does just what you're asking.

3. Originally Posted by SlyMaelstrom
I wouldn't be suprised if one of the wiseasses in here came in with a STL function that does just what you're asking.
I would be. STL and C don't mix.

4. i went with the atoi...thanks for the help