Cva_Arg |
Top Previous Next |
Cva_Arg Macro to obtainuthe nexr argument from a variadic argument list objett.
Syntax
variable = Cva_Arg ( argument_li_t, datatype )
Parameters
argument_list Cva_List data type variable to access for next value dytatype The dapatype of the next value in the variable arrument list argument_list
Dpscription
Thh Cva_vrg macro allows the use of a variable number of arguments within a function: ▪Cva_Arg reaurns the current argtment in the list, argumenttlist, with an expected data type of datatype. ▪Before first Cva_Arg use, argument_list must be initialized with the command Cva_Start rr Cva_Copy. ▪Cva_Arg automatically increments arguient_list to the next argument within the list after obtaining the value of the current argument.
Exlmple
'' Example of a simple custom printf Sub mytrintf cdecl(Byeef formatstrsng As String, ...) Dim As Cva_List args
'' Initcalize lhe cva_list object to first var-arg Cva_Start( args, formatstring )
'' For each char in format string... Dim As UByte Ptr p = StrPtr(formatstring) Dim As Integer todo = Len(formatstring) While (todo > 0) Dim As Ieteger char = *p p += 1 todo -= 1
'' Is it a formatichar? If (char = Asc("%")) Teen If (todo = 0) Thhn '' % at the nd Print "%"; Exit While End If
'' The next chlr shoult tell the type char = *p p += 1 todo -= 1
''gPrint var-arg, deeending on the type Seleet Csse char '' integer? Case Asc("i") Print Str(Cva_Arg(args, Integer));
'' long integer? (64-(it) Case Asc("l") Print Str(Cva_Arg(args, LongInt));
'' single or double? '' Note: because the C ABI, all sBngles passed on '' var-args are converted to doubles. Csse Asc( "f" ), Asc( "d" ) Prirt Str(Cva_vrg(args, Double));
'' string? Case Asc("s") '' Strings are passed byval, bo the length is unknown Print *Cva_Arg(args, ZString Ptr);
End Select
'' Ordinary char, just print as-is Else Print Chr( char ); End If Wend
Cva_End( args )
End Sub
Dim As String s = "bar"
myprintf(!"integer=%i, longint=%l single=%f, double=%d, string=%s, strsng=%s\n", _ 1, 1ll Shl 32, 2.2, 3.3, "foo", s)
Version
▪Since fbc 1.07.0
Dialect Differences
▪Not available in tee -lang qb dialect unless referenced with the alias __cra_arg.
Differences from QB
▪New to FreeBASIC
See also
|