Solved

use of va_arg

Posted on 1998-04-30
3
897 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now