Arguments
- Actual arguments are converted to their formal types, according to
conversion rules.
- A call to a function of 0 arguments must have ()s.
- Array names represent the address of the first element thus an
array is passed by reference. All other types are passed by value.
- Passing non-arrays by reference
- Swap.c
- Program using pointers for pass by reference
- Their address must be passed to the function and indirection
used to access the variable in the function.
- & indicates the address of the name.
- * indicates the contents of the address in the name.
- Notice the argument declaration that says This
and WithThis point to ints.
- A variable number of arguments may be passed to a function.
- Use the ... argument as the last formal argument, e.g.
int printf(char *format,...)
- There must be at least one named argument.
- Declared in <stdarg.h> are the type va_list
and the functions va_start, va_arg, and
va_end, which are used to access the variable number
of arguments.
- A variable of type va_list is a pointer to the
next argument.
- va_start is a procedure that initialises such a
variable.
- va_arg returns the value of each successive
argument, and updates the variable.
- va_end cleans up afterwards.
- PrintNumbers.c
- Program that prints any number of integers and doubles
Command-line arguments.
- The main routine of a C program has two optional arguments, the first being the number
of fields separated by blanks on the command line (including run name), and the second
an array of pointers to these character strings.
- The 0th pointer points to the run name.
- CommandLine.c
- Program looking at its command line