Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

use of va_arg

Posted on 1998-04-30
3
Medium Priority
?
914 Views
Last Modified: 2012-08-14
Can anyone explain how va_arg works and give an example?
0
Comment
Question by:mjgaudette
[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
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
rmichels earned 90 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

604 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