Welcome, Guest. Please login or register.

Author Topic: Functions with variable nr. of parameters Dealing with +1 argument....  (Read 958 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
Hi. THis is the 1st time I mess with this. I wanted to create a function that receives a ptr and a variable number of strings and then allocates enouph memry to the concatenation of all strings passing the address to the received ptr, and finally concatenates all strings in the order they are passed. To prove myself bellow is my first working piece (of art :lol: ) of code that does this.

Anyway, what I want to know is if there is a way to have va_arg() start passing arguments after the 2nd argument (eg start in the 3rd), or is this impossible ? The way I get the 3rd arguement the 2nd time (having to use va_arg() one time to pass by the 2nd first) seems a bit hackish...


Code: [Select]
#include <exec/memory.h>
#include <exec/types.h>
#include <stdio.h>
#include <stdarg.h>

WORD StringSize = 0;

BOOL AllocAndCpyStr (int num, char **dest, ...);

int main (int argc, char **argv)
{
 char *a = &quot;String a test, &quot;;
 char *b = &quot;string b test&quot;;
 char *c;


 if (AllocAndCpyStr (3, &c, a, b))
   printf (&quot;Sucess, concatenation and allocation result is:%s\n&quot;,c);
 else
   printf (&quot;Failed&quot;);

 if (*c)
   FreeMem (c, StringSize);
 exit (0);
}


/* DON'T FORGET 2 INCLUDE STDARG.H */
/* Allocates space 4 and copies all strings to dest, in order of arguments */
BOOL AllocAndCpyStr (int num, char **dest, ...)
{

 va_list argptr;
 BOOL Result = FALSE;
 char *String;
 char **Destination;
 int count;

 /* Initialize argptr */
 va_start (argptr, num);

 Destination = va_arg (argptr, char **);
 /* Allocate space 4 strings */
 for (count = num - 1; count; count--)
   { String = va_arg (argptr, char *);
       printf (&quot;string: %s\n&quot;, String);
     StringSize += strlen (String);
   }
 StringSize++; /* 4 the last NULL */
 printf (&quot;Allocated space is %d bytes\n\n&quot;, StringSize);
 if (!(*Destination = (char *)AllocMem (StringSize, MEMF_ANY | MEMF_CLEAR)))
   printf (&quot;Out of memory in report!\n&quot;);

 /* Copy over strings */
 else
   { va_end (argptr);
     va_start (argptr, num);

     va_arg (argptr, char **);
     for (num -= 1; num; num--)
       { strcat (*Destination, va_arg (argptr, char *));
       }
     Result = TRUE;
   }

 /* do orderly shutdown */
 va_end (argptr);

 return (Result);
}

\\"We made Amiga, they {bleep}ed it up\\"
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
Re: Functions with variable nr. of parameters Dealing with +1 argument....
« Reply #1 on: September 04, 2005, 08:49:42 PM »
BUMP....
come on, I wanted this doubt sorted before coding tonight... ;-)
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline Thomas

Re: Functions with variable nr. of parameters Dealing with +1 argument....
« Reply #2 on: September 04, 2005, 08:59:09 PM »

You should have done a Google search for stdarg.h or va_start before you ask. It is all well documented.

However, the va_start macro always gets the variable name before the "...".

So you should use va_start(argptr, dest);

The way you use it is wrong. I wonder why it works at all.

Bye,
Thomas

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
Re: Functions with variable nr. of parameters Dealing with +1 argument....
« Reply #3 on: September 06, 2005, 04:40:32 PM »
@Thomas

It worked because I noticed it was getting the second variable (now I know why, I was giving va_start the first one) so I used the va_arg call twice only using the result of the second call...
\\"We made Amiga, they {bleep}ed it up\\"