Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7787515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:41:50+00:00 2026-06-01T20:41:50+00:00

Suppose I want to make a function that recursively parses a variadic argument list,

  • 0

Suppose I want to make a function that recursively parses a variadic argument list, by letting each invocation of the function read the next argument? After handing the va_list to the next function, I am not intending to continue using the va_list in the calling function. Is the following code ok:

void VarArgRecursive( va_list args ) {
    int nextArg = va_arg(args, int);
    if( nextArg != -1 ) {
        printf("Next arg %d\n", nextArg);
        VarArgRecursive(args);
    }
}

void VarArgFunc( int firstArg, ... ) {
    va_list args;
    va_start(args, firstArg);
    VarArgRecursive(args);
    va_end(args);
}

int main (int argc, char * const argv[]) {

    VarArgFunc(20, 12, 13, -1);

    return 0;
}

The code compiles on my system, and the output is as expected:

Next arg 12
Next arg 13

So, is this practice OK? I have searched the list, and found that after handing the va_list over to the next function, the contents of the va_list in the calling function is undefined. That shouldn’t matter for my usage, as I will not continue using the va_list after handing it over to the next (well, actually, the same) function. I have also checked this page:

http://c-faq.com/varargs/handoff.html

…which shows that my way of handing over the va_list to the next function is OK. What it doesn’t say, is whether it is OK to hand the va_list over to yet another function after reading one arg, and expect the called function to read the next arg. If there are c++ -specific answers to this question, that is also ok, since it will be used in a c++ program.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:41:52+00:00Added an answer on June 1, 2026 at 8:41 pm

    You can pass it however many times you like, but you cannot “use” the va_list more than once. When consumed, the va_list may be modified and using it again is undefined behavior per the C++ spec.

    If you want to use it more than once, call va_copy to clone the va_list prior to consuming it, then pass the copy.

    However, in your case what you’re doing is acceptable. The problem arises when you attempt to pass the va_list from the beginning to another function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I want a function that takes a number and returns it as a
I'm trying to write a Powershell function that takes an array argument. I want
Suppose you want to make an async request in JavaScript, but you want to
Suppose I want to make a Web application which uses a fixed width and
Suppose I want to implement a reasonably efficient 'keyword recognition algorithm', that is first
Suppose I want to create a set of observers based on type. That is
Suppose I want to put objects that identify a server into a stl set
Is there any way to make a function call only once? Suppose I have
Suppose I have a function that does something on an arbitrary container type (C++11):
I'm want to make code that reads assembly instructions (x86 only) and recreates them

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.