Sie sind auf Seite 1von 3

12/3/2016

Howtouse__VA_ARGS__insideaCfunctioninsteadofmacro?StackOverflow

signup

login

tour

help

xDismiss

JointheStackOverflowCommunity
Stack Overflow is a community of 6.4 million
programmers, just like you, helping each other.
Join them it only takes a minute:
Signup

Howtouse__VA_ARGS__insideaCfunctioninsteadofmacro?[duplicate]

PossibleDuplicate:
C/C++:Passingvariablenumberofargumentsaround
I'mcurrentlyusingthefollowingmacrodeclaredonmyCfile.
#defineCOMMON_Print(...)printf(__VA_ARGS__)

Nowthatcallworksjustfine,butturnsoutthatIneedtobeabletocreateaCfunctionthatlookssomethinglikethis:
voidCOMMON_Print(...)
{
printf(__VA_ARGS__);
}

Sothatfunctiondoesn'twork,Igetanerror
"Error:undefinedidentifier__VA_ARGS__"
Thecomplexityofmyprojectrequirestohaveafunctionsinceit'saninterface...SohowcanIgettheparameters...andpassthemtotheprintf
function?OrbetterwhatamIdoingwrong?
Thanks!
c
askedDec2'10at20:16

Jona
6,549

60

106

markedasduplicatebyKos ,abelenky ,Let_Me_Be,nos ,JohnKugelmanDec2'10at20:25


Thisquestionhasbeenaskedbeforeandalreadyhasananswer.Ifthoseanswersdonotfullyaddressyourquestion,pleaseaskanewquestion.

1 Seethisstackoverflow.com/questions/205529/Kos Dec2'10at20:19

4Answers

Eachofthe ?printf functionshasacorresponding


thingbuttakesa va_list ,avariableargumentlist.

v?printf

functionwhichdoesthesame

#include<stdio.h>
#include<stdarg.h>
voidCOMMON_Print(char*format,...)

http://stackoverflow.com/questions/4339412/howtousevaargsinsideacfunctioninsteadofmacro

1/3

12/3/2016

Howtouse__VA_ARGS__insideaCfunctioninsteadofmacro?StackOverflow

{
va_listargs;
va_start(args,format);
vprintf(format,args);
va_end(args);
}

Sidenote:Noticethat va_start takesthenameofthelastargumentbeforethe ... asa


parameter. va_start isamacrowhichdoessomestackbasedmagictoretrievethevariable
argumentsanditneedstheaddressofthelastfixedargumenttodothis.
Asaconsequenceofthis,therehastobeatleastonefixedargumentsoyoucanpassitto
va_start .ThisiswhyIaddedthe format argumentinsteadofstickingwiththesimpler
COMMON_Print(...) declaration.
See:http://www.cplusplus.com/reference/clibrary/cstdio/vprintf/
editedDec2'10at20:25

answeredDec2'10at20:19

JohnKugelman
178k

36

310

388

Ifyouusemacros,youneednoprecedingarg.Considerthisexample: #defineLOGI(...)
((void)__android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)) Thisobviouslyisvery

convenientfromausageviewpoint.ArcaneEngineerJul28'15at11:39

__VA_ARGS__
va_start

isonlyformacrosvariadicfunctionsareratherdifferent.Youneedtouse
and va_end from stdarg.h tohandlethem.

va_arg

First,yourfunctionneedsatleastonenamedparameter,e.g.:
voidCOMMON_Print(constchar*fmt,...)

Thenyoucandefinea

va_list

anduse

va_start

tosetit:

va_listargs;
va_start(args,fmt);
//yourcodehere
va_end(args);

Nowyoucanuse args toaccesstheargumentscalling va_arg(args,TYPE) willreturnthenext


variadicargument,soyoucanjustkeepcallingthatuntilyou'vereadallthearguments.
Ifyou'rejusttryingtocall
directly:

printf

,there'saprintfvariantcalled

vprintf

thattakesthe

va_list

vprintf(fmt,args);

Thereisnoeasywaytocallonevariadicfunctionfromanotheryouneedsomethinglike
vprintf thattakesthewhole va_list
editedDec2'10at21:07

answeredDec2'10at20:21

MichaelMrozek
74k

11

125

138

Thanksforyourexplanation.Thanksforthehelpandexplanationstheyreallyhelped!:) Jona Dec2'10

at20:33
'va_arg(args,TYPE)'willreturnNULLonlyifNULLwaspassedbythecaller.Thereisnodefaultend

conditionforva_arg.Youmusteitherpassasentinelvalueorknowtheexactnumberofparameters,passed
byaparameterorbycountingthenumberof%inaprintfformatstring.PatrickSchlterDec2'10at21:05
@tristopiaOh,rightI'minthehabitofpassingNULLattheendwhennecessary.Fixed MichaelMrozek

Dec2'10at21:08

__VA_ARGS__

isformacrosonly.

Chainingthevariablenumberofargumenttoanotherfunctioncan'tbedonedirectly.Insteadyou
havetopassava_list,andthefunctionyou'recallinghavetotakeava_list.Luckilythere'sa
variationofprintfthatdoesthis,yourfunctionhavetobewrittenthisway:
voidCOMMON_Print(char*format,...)
{
va_listargs;
va_begin(args,format);
vsprintf(format,args);

http://stackoverflow.com/questions/4339412/howtousevaargsinsideacfunctioninsteadofmacro

2/3

12/3/2016

Howtouse__VA_ARGS__insideaCfunctioninsteadofmacro?StackOverflow

va_end(args);
}
editedDec1'15at14:43

answeredDec2'10at20:22

nos
143k

33

241

378

1 Thanksalotforyourresponse!Itactuallyworks!ButtherearesomeformattingissuesgoingonthatIneed
toresolveonmyendIbelieve.FYI,I'mworkingonanembeddeddevice. Jona Dec2'10at20:32
Whatisvaprintf?Orisitjustatypo? VincentFourmond Dec1'15at14:42

WhatyouarelookingforistheEllipsis operator.
editedMay20'14at18:06

answeredDec2'10at20:22

Dima
29.5k

11

54

98

http://stackoverflow.com/questions/4339412/howtousevaargsinsideacfunctioninsteadofmacro

3/3

Das könnte Ihnen auch gefallen