Variable arguments in C, how to get values with a generic type?

前端 未结 6 1639
花落未央
花落未央 2020-12-01 21:00

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 functi

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-01 22:01

    You can't do this with variable arguments the way you're describing because no information about the type of the arguments you pass is retained after compilation unless you do it explicitly. Even passing the address of the argument variable won't tell you this, because the bits in memory that represent a string could represent a number or something else.

    To make varargs work with variable types, you can store type information in the arguments themselves (e.g., as described by jldupont in his answer), or you can store the information in a non-variable argument (e.g., a format string like printf's).

提交回复
热议问题