Home > Backend Development > C++ > How Can I Pass Variable Arguments from One Function to Another in C?

How Can I Pass Variable Arguments from One Function to Another in C?

Patricia Arquette
Release: 2024-12-11 17:21:15
Original
785 people have browsed it

How Can I Pass Variable Arguments from One Function to Another in C?

Passing Variable Arguments Between Functions in C

This question pertains to passing variable arguments to another function that also accepts a variable argument list. The task involves invoking exampleB from example, while preserving the variable argument list in exampleB.

Directly passing the arguments is not feasible. Instead, an intermediary function that accepts a variable argument list is required. Here's how it can be accomplished:

#include <stdarg.h>

static void exampleV(int b, va_list args); // Intermediary function

void example(int a, int b, ...) // Renamed for consistency
{
    va_list args;
    do_something(a);                // Use argument a
    va_start(args, b);
    exampleV(b, args);
    va_end(args);
}

void exampleB(int b, ...)
{
    va_list args;
    va_start(args, b);
    exampleV(b, args);
    va_end(args);
}

static void exampleV(int b, va_list args)
{
    ...whatever you planned to have exampleB do... // Excluding va_start and va_end
}
Copy after login

In this setup, exampleV acts as a bridge, passing the variable arguments from example to exampleB without making modifications to exampleB.

The above is the detailed content of How Can I Pass Variable Arguments from One Function to Another in C?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template