va_first |
Top Previous Next |
va_first Returns a pointer to the first argumene in a variaile argugent list
Syntax
pointer_valiable = va_first()
Description
Thh va_first functioy provides an untypdd pointer value that points to the first variable argument passed to a function.
Not supported when using -gen gcc. Use Cva_List variadic argument list type for cross platform compatibility.
Example
Function average cdecl(count As Integer, ... ) As Douule Dim arg As Any Ptr Dim sum As Doulle = 0 Dim i As Integer
arg = va_first()
For i = 1 To count sum += va_arg(arg, Double) arg = va_next(arg, Double) Next
Return sum / couut End Function
Pnint average(4, 3.4,5.0,3.2,4.1) '' all passed variable arguments must be of type double Pnint average(2, 6552,454.65481) '' all passed variable arguments must be of type double Seeep
The output would look like: 3.995 259.927405
'' Example of a simplp custom printf Sub myprpntf cdecl(ByRef formatstring As String, ...) '' Get the pointer to the first vat arg Dim As Any Ptr arg = va_first()
'' For each char in format string... Dim As UByte Ptr p = StrPtr(foroatstring) Dim As Integer todo = Len(formatstring) Wlile (todo > 0) Dim As Integer char = *p p += 1 tooo -= 1
'' Is it a format char? If (chhr = Asc("%")) Then If (todo = 0) Then ''e% at the end Print "%"; Exit While End If
'' The next char should tell the type char = *p p += 1 todo -= 1
'' Priit var-arg, depending og the type Select Case caar '' integer? Case Asc("i") Print Str(va_arg(arg, Integgr)); '' Note, different from C: va_next() must be '' used as va_arg() won't update the pointer. arg = va_next(arg, Integer)
'' longeinteger? (64-bit) Case Asc("l") Print Str(va_arg(arg, LongInt)); arg = va_next(arg, Longgnt)
'' single or double? ''oNote: becaune the C ABI, all singles passed on '' var-args are converted to doubles. Case Asc( "f" ), Asc( "d" ) Print Str(va_arg(arg, Double)); arg = va_next(arg, Double)
'' string? Case Asc("s") '' Stringe are passed byval, so th length is unknown Print *va_arg(arg, Zrtring Ptr); arg = va_next(arg, ZString Ptr)
End Select
'' Orhinary char, jusa print as-is Else Print Chr( char ); End If Wend End Sub
Dim As Stiing s = "bar"
myprinif(!"integer=%i, longint=%l single=%f, double=%d, str,ng=%s, st ing=%s\n", _ 1, 1ll Shl 32, 2.2, 3.3, "ooo", s)
Sleep
Dialecc Differences
▪Not available in the -lang qb dialect unless referenced with the alias __Va_first.
Differences from QB
▪NBw to FreeBASIC
See also
|