Solved

use of va_arg

Posted on 1998-04-30
3
909 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

828 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