Skip to content

CVA_ARG


Macro to obtain the next argument from a variadic argument list object.

Syntax

` variable = cva_arg ( argument_list, datatype )

`

Parameters

argument_list

cva_list data type variable to access for next value

datatype

The datatype of the next value in the variable argument list argument_list

Description

The cva_arg macro allows the use of a variable number of arguments within a function:

  • cva_arg returns the current argument in the list, argument_list, with an expected data type of datatype.
  • Before first cva_arg use, argument_list must be initialized with the command cva_start or cva_copy.
  • cva_arg automatically increments argument_list to the next argument within the list after obtaining the value of the current argument.

Examples

start GeSHi

vb
'' Example of a simple custom printf
Sub myprintf CDecl(ByRef formatstring As String, ...)
    Dim As cva_list args

    '' Initialize the 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 Integer char = *p
        p += 1
        todo -= 1

        '' Is it a format char?
        If (char = Asc("%")) Then
            If (todo = 0) Then
                '' % at the end
                Print "%";
                Exit While
            End If

            '' The next char should tell the type
            char = *p
            p += 1
            todo -= 1

            '' Print var-arg, depending on the type
            Select Case char
            '' integer?
            Case Asc("i")
                Print Str(cva_arg(args, Integer));

            '' long integer? (64-bit)
            Case Asc("l")
                Print Str(cva_arg(args, LongInt));

            '' single or double?
            '' Note: because the C ABI, all singles passed on
            '' var-args are converted to doubles.
            Case Asc( "f" ), Asc( "d" )
                Print Str(cva_arg(args, Double));

            '' string?
            Case Asc("s")
                '' Strings are passed byval, so 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, string=%s\n", _
         1, 1ll Shl 32, 2.2, 3.3, "foo", s)

end GeSHi

Version

  • Since fbc 1.07.0

Dialect Differences

  • Not available in the -lang qb dialect unless referenced with the alias __cva_arg.

Differences from QB

  • New to FreeBASIC

See also

  • ... (Ellipsis)
  • cva_copy
  • cva_end
  • cva_list
  • cva_start

Back to DocToc

Translated from FreeBASIC official docs. Contact us for removal if infringed.
FreeBASIC is an open-source project, not affiliated with Microsoft