Please answer by e-mail if possible...
#include <stdarg.h>
#include <string.h>
/*
* str_chain: chains several strings together, including allocating
* space for them.
*/
char *str_chain( int num, ... )
{
static char (*str_table)[256];
int lengths[256], tot_len, i, j;
char *r, *result;
va_list ap;
va_start( ap, num );
for( i=0, tot_len=1; i<num; i++)
{
/*
* Here's the line where I get several errors. Now, if I
* enclose the (char *) in parentheses, I get even more
* errors, including about 4 "Parse error before ')'"'s.
* As is this gives "Incompatible types in assignment."
*/
str_table[i] = va_arg( ap, char *);
tot_len += (lengths[i]=strlen(str_table[i]));
}
r = result = (char *) malloc( tot_len * sizeof( char ));
for (i=0; i < num; i++)
{
strcpy( str_table[i], r );
r += lengths[i];
}
va_end( ap );
return result;
}
--
+-------------------------------------+----------------------------------------+
| Kenneth Platz Aurik the Bold | He who indulges in sensuous pleasures |
| kjp4...@uxa.cso.uiuc.edu | with luscious women may not be morally |
| Au...@uiuc.edu | correct, but he can have a lot more fun|+-------------------------------------+----------------------------------------+
--
Jerry Feldman Unix Systems Group
Digital Equipment Corp. Mailstop: ZKO3-3/Y25
110 Spitbrook Rd. g...@zk3.dec.com
Nashua, NH 03062-9987 (603)881-2970
> Hi guys -- I'm having a little problems with a function that
> uses <stdarg.h> I've tried it on several different compilers,
> including gcc for SunOS, Sequent, and AIX. Is there
> anything that I'm obviously doing wrong?
>
> Please answer by e-mail if possible...
>
>
> #include <stdarg.h>
> #include <string.h>
> /*
> * str_chain: chains several strings together, including allocating
> * space for them.
> */
> char *str_chain( int num, ... )
> {
> static char (*str_table)[256];
^ ^
I don't know what the effect of these parentheses is,
but I never put 'm there.
lets see
char *x[N]; is an array of N pointers to char
char *(x[N]); is an array of N pointers to char
char (*x)[N]; is a pointer to an array of N chars <<<<< I think
char (*x)(); is a pointer to a function returning char
kill me if I'm wrong, I am never sure when declaring strange declerations,
I always have to take a look at the assembler output of my compiler before
I decide I will use one.
(char *) (x[N]); does this have meaning ???
> int lengths[256], tot_len, i, j;
> char *r, *result;
> va_list ap;
>
> va_start( ap, num );
>
> for( i=0, tot_len=1; i<num; i++)
> {
> /*
> * Here's the line where I get several errors. Now, if I
> * enclose the (char *) in parentheses, I get even more
that's because in the macro that implements va_arg there is
lots of sizeof(type) used. when adding parentheses it will say
sizeof((type)) which is the size of a type cast, something which
is not legal.
> * errors, including about 4 "Parse error before ')'"'s.
> * As is this gives "Incompatible types in assignment."
> */
> str_table[i] = va_arg( ap, char *);
> tot_len += (lengths[i]=strlen(str_table[i]));
> }
> r = result = (char *) malloc( tot_len * sizeof( char ));
> for (i=0; i < num; i++)
> {
> strcpy( str_table[i], r );
> r += lengths[i];
> }
> va_end( ap );
> return result;
> }
willem