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

  • Home
  • SEARCH
  • 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 8330321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:05:22+00:00 2026-06-09T02:05:22+00:00

I want to create a function pointer to a function that will handle a

  • 0

I want to create a function pointer to a function that will handle a subset of cases for a function that takes a variable parameter list. The use case is casting a function that takes ... to a function that takes a specific list of parameters, so you can deal with variable parameters without dealing with va_list and friends.

In the following example code, I’m casting a function with a variable parameters to a function with a hard-coded parameter list (and vice versa). This works (or happens to work), but I don’t know if’s a coincidence due to the calling convention in use. (I tried it on two different x86_64-based platforms.)

#include <stdio.h>
#include <stdarg.h>

void function1(char* s, ...)
{
    va_list ap;
    int tmp;

    va_start(ap, s);
    tmp = va_arg(ap, int);
    printf(s, tmp);
    va_end(ap);
}

void function2(char* s, int d)
{
    printf(s, d);
}

typedef void (*functionX_t)(char*, int);
typedef void (*functionY_t)(char*, ...);

int main(int argc, char* argv[])
{
    int x = 42;

    /* swap! */
    functionX_t functionX = (functionX_t) function1;
    functionY_t functionY = (functionY_t) function2;

    function1("%d\n", x);
    function2("%d\n", x);
    functionX("%d\n", x);
    functionY("%d\n", x);

    return 0;
}

Is this undefined behavior? If yes, can anyone give an example of a platform where this won’t work, or a way to tweak my example in such a way that it will fail, given a more complex use case?


Edit: To address the implication that this code would break with more complex arguments, I extended my example:

#include <stdio.h>
#include <stdarg.h>

struct crazy
{
    float f;
    double lf;
    int d;
    unsigned int ua[2];
    char* s;
};

void function1(char* s, ...)
{
    va_list ap;
    struct crazy c;

    va_start(ap, s);
    c = va_arg(ap, struct crazy);
    printf(s, c.s, c.f, c.lf, c.d, c.ua[0], c.ua[1]);
    va_end(ap);
}

void function2(char* s, struct crazy c)
{
    printf(s, c.s, c.f, c.lf, c.d, c.ua[0], c.ua[1]);
}

typedef void (*functionX_t)(char*, struct crazy);
typedef void (*functionY_t)(char*, ...);

int main(int argc, char* argv[])
{
    struct crazy c = 
    {
        .f = 3.14,
        .lf = 3.1415,
        .d = -42,
        .ua = { 0, 42 },
        .s = "this is crazy"
    };


    /* swap! */
    functionX_t functionX = (functionX_t) function1;
    functionY_t functionY = (functionY_t) function2;

    function1("%s %f %lf %d %u %u\n", c);
    function2("%s %f %lf %d %u %u\n", c);
    functionX("%s %f %lf %d %u %u\n", c);
    functionY("%s %f %lf %d %u %u\n", c);

    return 0;
}

It still works. Can anyone point out a specific example of when this would fail?

$ gcc -Wall -g -o varargs -O9 varargs.c
$ ./varargs
this is crazy 3.140000 3.141500 -42 0 42
this is crazy 3.140000 3.141500 -42 0 42
this is crazy 3.140000 3.141500 -42 0 42
this is crazy 3.140000 3.141500 -42 0 42
  • 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-09T02:05:23+00:00Added an answer on June 9, 2026 at 2:05 am

    Casting the pointer to a different function pointer type is perfectly safe. But the only thing that the language guarantees is that you can later cast it back to the original type and obtain the original pointer value.

    Calling the function through a pointer forcefully converted to incompatible function pointer type leads to undefined behavior. This applies to all incompatible function pointer types, regardless of whether they are variadic or not.

    The code you posted produces undefined behavior: not at the point of the cast, but at the point of the call.


    Trying to chase examples “where it would fail” is a pointless endeavor, but it should be easy anyway, since parameter passing conventions (both low-level and language-level) are vastly different. For example, the code below normally will not “work” in practice

    void foo(const char *s, float f) { printf(s, f); }
    
    int main() {
      typedef void (*T)(const char *s, ...);
      T p = (T) foo;
      float f = 0.5;
      p("%f\n", f);
    }
    

    Prints zero instead of 0.5 (GCC)

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

Sidebar

Related Questions

I want to create a function that takes a 2 dimensional list and outputs
I want to create a function that will cycle through 3 blocks of DIV's
I want to create stored procedure that call recursion function, each time it will
I want to create some function for rendering images from within Joomla v1.5.23. That
I want to create a function such that each time the function runs, the
I'm working in PHP and I want to create a function that, given a
I want to create a postgres function that builds the set of columns it
Hey so I'm making a serialization function that takes a base class pointer 'Joint'
I am trying to create a property that will make a pointer (byte*) from
I want to create a function which plot on screen a set of figures

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.