Solved

use of va_arg

Posted on 1998-04-30
3
907 Views
Last Modified: 2012-08-14
Can anyone explain how va_arg works and give an example?
0
Comment
Question by:mjgaudette
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
rmichels earned 30 total points
ID: 1256683
va_arg is a way of passing in variable arguments to a function.

If you have ever used printf() or sprintf() you have seen variable arguments in use.  The following example code is for Microsoft C

#include <stdarg.h> // get the va_arg marcro
#include <stdlib.h>
#include <string.h>
int TestFunction(char *str, ...);

void Example(void)
{
char c[100];
int i,j;

strcpy(c,"dummy string");

TestFunction(c,i,j);
TestFunction(c,j);
TestFunction(c,i,i,j);

}

int TestFunction(char *str, ...)
{
  va_list vlist;
  int i,j,k,l;

  va_start(vlist, str);  //initialize access to the list

  i = va_arg(vlist, int);

  if (i != -1)
    // variable is there in list
 

  va_end(vlist); //terminate use of list
}

you use va_start and va_end to initialize access to the variable list.  Then use va_arg to get the next variable passed in.  You must know the variable type you are expecting.  For example, the printf function knows the variable types based on the formatting information in the format string.
0
 

Author Comment

by:mjgaudette
ID: 1256684
So assuming the variables are all passed on the stack, va_start returns the address of the first argument and va_arg moves backwards through the list until it gets to the last argument?
0
 
LVL 3

Expert Comment

by:rmichels
ID: 1256685
yes that is correct, you tell va_start the last fixed parameter(before the optional parameters start).  You then tell va_arg the type of variable to get, so it knows the size on the stack.  I do not use these macros much, but they do come in handy.  
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c++ getting the first 10 characters of a char* string 11 93
Acrinis True image 2 77
gdb doesn't stop on breakpoint 2 67
NEED HELP WITH VISUAL STUDIO 2017 (beginner) 6 38
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

776 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