Solved

variable-argument lists

Posted on 2000-03-18
9
308 Views
Last Modified: 2010-05-18
Here's my question:  I'm trying to create a custom printf-like function that processed a string as input that also contains format specifiers.  Obviously this function requires a variable-length argument list.

Everything works out fine, except for one thing:  When I run the program (the one below is a fragment of my actual program), the printf-like function processes and displays the FIRST argument correctly.  Second and subsequent arguments don't get displayed properly (ex. garbage on screen).  What to do??

////////////////////////////////////////
vector<string> g_vecStrStats; //global

template<class T>
void gAppendStats(string szStat, int iCount, T pvFirst, ...)
{
   string  szResult= szStat;
   TCHAR   sz[50];
   va_list ap;
   int     iCurrPos= 0;
   int     iCurrPos2;

   va_start(ap, iCount);
   T pv= va_arg(ap, T);

   //Skip all chars until a '%' is
   //found.
   while(szResult[iCurrPos] != '%')
      ++iCurrPos;

   //iCurrPos should now represent the
   //first '%' position.
   iCurrPos2= iCurrPos;
   while(iCurrPos2 != szResult.length())
   {
      if (szResult[iCurrPos2] == 'd')
      {
         ltoa(pv, sz, 10);
         szResult.replace(iCurrPos,
         iCurrPos2 - iCurrPos + 1, sz,
         0, strlen(sz));
         break;
      }

      else if (szResult[iCurrPos2] ==
               'f')
      {
         gcvt(pv, 5, sz);
         szResult.replace(iCurrPos,
         iCurrPos2 - iCurrPos + 1, sz,
         0, strlen(sz));
         break;
      }

      ++iCurrPos2;
   }
   iCurrPos= iCurrPos2;

   //Process second and subsequent
   //variable args.
   for (int i= 1; i < iCount; i++)
   {
      T pv= va_arg(ap, T);

      //Skip all chars until a '%' is
      //found.
      while(szResult[iCurrPos] != '%')
         ++iCurrPos;

      //iCurrPos should now represent
      //the next '%' position.
      iCurrPos2= iCurrPos;
      while(iCurrPos2 != szResult.length
      ())
      {
         if (szResult[iCurrPos2] == 'd')
         {
            ltoa(pv, sz, 10);
            szResult.replace(iCurrPos,
            iCurrPos2 - iCurrPos + 1,
            sz, 0, strlen(sz));
            break;
         }

         else if (szResult[iCurrPos2]
                  == 'f')
         {
            gcvt(pv, 5, sz);
            szResult.replace(iCurrPos,
            iCurrPos2 - iCurrPos + 1,
            sz, 0, strlen(sz));
            break;
         }

         ++iCurrPos2;
      }
      iCurrPos= iCurrPos2;
   }

   //Finally, add this processed string
   //stat in the vector of strings and
   //perform cleanup.
   g_vecStrStats.push_back(szResult);
   va_end(ap);
}

void main()
{
   long  lNumBufs = 2;
   float fBitNum  = 16.578f;

   gAppendStats("Detected %d %f-bit Z
   buffers", 2, lNumBufs, fBitNum);

   //app cleanup
   g_vecStrStats.clear();
}
0
Comment
Question by:metal3dx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 1

Accepted Solution

by:
ramshank earned 50 total points
ID: 2632646
try giving the last specifed arguement in the list pvFirst in va_start instead of iCount.

va_start(ap, iCount);
the follwing the definition of the macro

void va_start( va_list arg_ptr, prev_param );  

va_start sets arg_ptr to the first optional argument in the list of arguments passed to the function.  The argument prev_param is the name of the required parameter immediately preceding the first optional argument in the argument list. If prev_param is declared with the register storage class, the macro’s behavior is undefined.

In the code that you had specified you pass iCount which is not the required parameter immediately preceeding the optional parameter list.  Try changing that to pvFirst
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 2636444
Hi. I tested you example:
 You can't use in same loop long/float!
>>   T pv= va_arg(ap, T);
How translator knows, what is type of this ?
Of course, by you declaration : "T".(long in example)
>>   gAppendStats("buffers", 2, lNumBufs, fBitNum);
but in same example next variable has type float(fBitNum)
and this is the reason of error!
For fix it you must make ALL float/int/long/void/void *, ...
Alex
0
 

Author Comment

by:metal3dx
ID: 2637425
To ramshank:

I've tried your idea before and the program still gave me incorrect results.

To AlexVirochovsky:

I've also tried your idea also.  As a matter of fact, using void* will give me a compiler error (at function gcvt, I will need a typecast from 'void*' to 'float' which is not allowed.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 2638855
Try use char * as printf
0
 

Author Comment

by:metal3dx
ID: 2640736
Maybe I should restate the problem:

How do I create a printf-like function?
Obviously, a printf-like function will
have a variable length argument list AND is probably a template function so that it can support multiple basic data types.
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 2644682
Printf don't use template and use char *
as type of all parameters.
After that Printf tests format parameter and by this info detects real type of other parameters and convert to
symbol.
0
 

Author Comment

by:metal3dx
ID: 2662647
Does anybody out there have any sample code that demonstrates functions using variable argument lists like printf?
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 2662880
Next is an example from BC doc:
/* va_arg example */

#include <stdio.h>
#include <stdarg.h>

/* calculate sum of a 0 terminated list */
void sum(char *msg, ...)
{
   int total = 0;
   va_list ap;
   int arg;
   va_start(ap, msg);
   while ((arg = va_arg(ap,int)) != 0) {
      total += arg;
   }
   printf(msg, total);
   va_end(ap);
}

int main(void) {
   sum("The total of 1+2+3+4 is %d\n", 1,2,3,4,0);
   return 0;
}
Borland C++ 5.0 Programmer's Guide
0
 

Author Comment

by:metal3dx
ID: 2665421
Not what I was looking for but you still answered the question.

Nice job!!!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question