Problems with <stdarg.h>

0 views
Skip to first unread message

Aurik of Westhampton

unread,
Mar 24, 1994, 4:06:24 AM3/24/94
to
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];
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|+-------------------------------------+----------------------------------------+

Gerald Feldman USG

unread,
Mar 25, 1994, 4:52:35 PM3/25/94
to

|> In article <2mrl6g$g...@vixen.cso.uiuc.edu>, kjp4...@uxa.cso.uiuc.edu (Aurik of Westhampton) writes:
|> 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];
Your problem is here: What you have here is str_table is a pointer to 256
characters. Take out the parens. What you want is str_table to be an array
of pointers to char.

|> 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;
|> }
--
Jerry Feldman

--
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

Root Account

unread,
Mar 25, 1994, 5:56:31 PM3/25/94
to
kjp4...@uxa.cso.uiuc.edu (Aurik of Westhampton) writes:

> 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

Reply all
Reply to author
Forward
0 new messages