1. ## concatenate

Hi guys. I wanted to build a function with a variable number of arguments that is able to concatenate in the same vector the vectors given as input (that can be 2,3, 100). I was thinking to something like this
Code:
```void concatenate(double *res, double *vec1, int length_vec1, ...)
{

}```
I tried to use va_args/start ecc but they seem (up to me) to handle only scalars...Has anyone a hint?

2. You've to make sure that res has enough space.
scalars ? Are you perl programmer?! :P
Code:
```
va_list ap;
va_start(ap,length_vec1);
arg = va_arg(ap,double*);```

3. Originally Posted by Bayint Naung
You've to make sure that res has enough space.
scalars ? Are you perl programmer?! :P
Code:
```
va_list ap;
va_start(ap,length_vec1);
arg = va_arg(ap,double*);```
OOOOOOOOK! For res there are no problems...No, I am only a poor statistician that is reaching for speed :-) . Now I am going to use your hint!
Thanks a lot!

4. I wonder if is it possible to handle vectors with variable length, I mean
Code:
```void concatenate(double *res, double *vec1, int length_vec1, double *vec2, int length_vec2)
{
...
}```
?

5. What are you trying to do?

6. I am trying to do a function that is able to concatenate a variable number of vectors, e.i.
Code:
```concatenate(res,vec1,length_vec1,vec2 ,length_vec2) or
concatenate(res,vec1,length_vec1,vec2 ,length_vec2, vec3 ,length_vec3)```
and so on. Maybe is more feasible to group the length of the vectors
Code:
`concatenate(res,length_vec1,length_vec2,vec,vec21)`

7. E.g variable argument function.
Code:
```void show_strs(char *s,...)
{
va_list ap;
va_start(ap,s);
do {
printf ("%s\n",s);
s = va_arg(ap,char * );
} while( s != NULL );
va_end(ap);

}
call it like this:

show_strs("hello", "world","variable",(char*) 0 );
show_strs("only one string",(char*) 0 );
show_strs("This is a ","1st","2nd","3rd","4th","end",(char*) 0);```

8. Originally Posted by Bayint Naung
E.g variable argument function.
Code:
```void show_strs(char *s,...)
{
va_list ap;
va_start(ap,s);
do {
printf ("%s\n",s);
s = va_arg(ap,char * );
} while( s != NULL );
va_end(ap);

}
call it like this:

show_strs("hello", "world","variable",(char*) 0 );
show_strs("only one string",(char*) 0 );
show_strs("This is a ","1st","2nd","3rd","4th","end",(char*) 0);```
of course, but with the difference that I use only arrays of numbers. This is, up to me, the main difficulty I have found in doing this function.

9. Post your code snippet and a sample of the data you're trying to manipulate.

10. Originally Posted by itCbitC
Post your code snippet and a sample of the data you're trying to manipulate.
ok...I post you my barbarian solution ;-) to the problem. Here it is the case where I have to concatenate 3 vectors
Code:
```void cb_c3(double *res, int *n1, int *n2, int *n3, double *vec1, double *vec2, double *vec3)
{
int i;
for( i=0 ; i<*n1 ; i++) res[i] = vec1[i];
for( i=0 ; i<*n2 ; i++) res[i+*n1] = vec2[i];
for( i=0 ; i<*n3 ; i++) res[i+*n1+*n2] = vec3[i];
}```
Now, my standard glod will be a function tha looks like this
Code:
```void cb_c3(double *res,  int n1, double *vec1, ...)
{
...
}```
where I put the different lengths of the vectors and the first pointer to the vector.
Do you understand? Does anyone knows R? There is a function called c() that concatenates vectors or scalars in the order they appear. This is what I want

11. You want to put together different types of vectors, as they are called in mathematics, to a single one (note the emphasis on types)? Or just the same type, and if so, what type?

12. Originally Posted by Elysia
You want to put together different types of vectors, as they are called in mathematics, to a single one (note the emphasis on types)? Or just the same type, and if so, what type?
I want to pool only vectors of doubles or integers

13. Originally Posted by violatro
where I put the different lengths of the vectors and the first pointer to the vector.
Do you understand? Does anyone knows R? There is a function called c() that concatenates vectors or scalars in the order they appear. This is what I want
Yeah, I know R and understand.

With variable argument lists, C does not have any means to implicitly detect the number of arguments passed. You'll need to either pass the number of vectors explicitly or look for a marker indicating the last vector.

A function of the form;
Code:
`void c(double *result, int number_of_vectors, ...)`
will do the trick. After the number_of_vectors argument, interpret the arguments in pairs (eg an int specifying number of elements in the vector, a pointer to the first element).

14. Originally Posted by violatro
I want to pool only vectors of doubles or integers
Ever considered you are using the wrong language? This would be easier (but still as fast) if you are using, say C++.

15. Originally Posted by grumpy
Yeah, I know R and understand.

With variable argument lists, C does not have any means to implicitly detect the number of arguments passed. You'll need to either pass the number of vectors explicitly or look for a marker indicating the last vector.

A function of the form;
Code:
`void c(double *result, int number_of_vectors, ...)`
will do the trick. After the number_of_vectors argument, interpret the arguments in pairs (eg an int specifying number of elements in the vector, a pointer to the first element).
What it is not clear is how to tell C with va_ that I am handling vectors and not scalars. All the examples I have seen deals with scalars or with strings.