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 436667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:30:03+00:00 2026-05-12T20:30:03+00:00

I’m trying to use C stdarg.h lib, with a generic type. The type int,

  • 0

I’m trying to use C stdarg.h lib, with a generic type.
The type int, is my generic type > to understand it, please, hold reading.
So, my problem is:

I have a function that accept variable number of arguments. like

void function (int paramN, ...);

In my program, there are no way to know, which is the type of variable arguments, it can be a char, an array, an int, an short, an function point, etc… like

function (paramN, "Hey, I'm a string", 1, function_pint, array, -1); // -1 is a sentinel.

So, I think that, an int, is 32bits, in a x86(32bits) system, this will hold all address of memory. So, if I get all arguments with a int, it will not be a problem, for example, “Hey, I’m a string” the address of this string, fit normally in a 32bits variable, so, i need only to make a cast.

Im right?
Can I do it?
Note: I don’t want to make my function like printf (this solution, don’t fit in this case ok?)

Thanks for all answer.
And sorry for my bad english.

  • 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-05-12T20:30:04+00:00Added an answer on May 12, 2026 at 8:30 pm

    You can’t do it the way you describe it.

    The C calling convention is for the caller to put arguments on the stack but it does not put any information on types, so the callee must have a way to find it (at least size of variables).

    • No problem for functions with prototypes every type is known.

    • With functions with variable number or parameters (variadic) it’s more tricky, you have to call va_arg for each argument to read each variable and you must provide the type for va_arg. If you provide a type that is not the real one the compiler won’t complain (it can’t it’s runtime information) but anything can happen (usually something bad).

    Hence you have to pass the type.

    In some cases you can predict type (eg: a counter followed by some ints, and so on), but usually you pass it encoded in parameters. You can pass it encoded in a format string like printf does, the union trick described by jldupont is also common enough.

    But anyway you have to pass it.

    You really can’t rely on underlying data binary representation, not even data size. Even if the program seems to work when you write it, it has no compatibility and can break with any change of the system, of the compiler, or even when changing compiling options.

    Let’s go with an example where you pass the type followed by the argument (thus neither the union trick nor the format string like printf). What it does is converting all it’s passed value to double and add them, no really useful isn’t it:

    #include <stdio.h>
    #include <stdarg.h>
    
    enum mytypes {LONG, INT, FLOAT, DOUBLE };
    
    double myfunc(int count, ...){
        long tmp_l;
        int tmp_i;
        double tmp_d;
        double res = 0;
        int i;
    
        va_list ap;
        va_start(ap, count);
        for(i=0 ; i < count; i++){
            int type = va_arg(ap, enum mytypes);
            switch (type){
                case LONG:
                tmp_l = va_arg(ap, long);
                res += tmp_l;
                break;
                case INT:
                tmp_i = va_arg(ap, int);
                res += tmp_i;
                break;
                case FLOAT:
                /* float is automatically promoted to double when passed to va_arg */
                case DOUBLE:
                tmp_d = va_arg(ap, double);
                res += tmp_d;
                break;
                default: /* unknown type */
                break;
            }
        }
        va_end(ap);
        return res;
    }
    
    int main(){
        double res;
        res = myfunc(5,
            LONG, (long)1,
            INT, (int)10,
            DOUBLE, (double)2.5,
            DOUBLE, (double)0.1,
            FLOAT, (float)0.3);
        printf("res = %f\n", res);
    }
    

    This exemple use the new stdarg variadic header defined in C99. To use it you need to have at least one fixed parameter to your function (in this exemple it’s count). The good thing if that thus you can have several variadic lists in your function (ie something like myfunc(int count1, ..., int count2, ...)). The bad thing is that you can’t have a purely variadic function (ie something like myfunc(…) like with the old format. You can still use the old format using varargs compatibility headers. But it is more complicated and rarely necessary, because you need types but also some way to know the list is finished and something like count is handy (but not the only way to do it, a ‘terminator’ could be used for instance).

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

Sidebar

Ask A Question

Stats

  • Questions 271k
  • Answers 271k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In IB there's "Clear when editing begins" checkbox for this… May 13, 2026 at 1:47 pm
  • Editorial Team
    Editorial Team added an answer A one-liner: $a = array_map('strval', $a); // strval is a… May 13, 2026 at 1:47 pm
  • Editorial Team
    Editorial Team added an answer I think in this case you should consider offline caching:… May 13, 2026 at 1:47 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.