?
Solved

use of va_arg

Posted on 1998-04-30
3
Medium Priority
?
912 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

Independent Software Vendors: 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

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…
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 how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

801 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