for example, if i want to create a funcion similar to printf:
Code: Select all
void prinft(char *string1, char *string2, );
Code: Select all
void prinft(char *string1, char *string2, );
Code: Select all
void foo(int something, ...);
Code: Select all
#include <stdarg.h>
void foo( int last_known_parameter, ... )
{
// an oblique data object doing "the magic"
va_list ap;
// initialize ap to point at the right location on the stack
va_start( ap, last_known_parameter );
// read an argument of type double from the stack
double x = va_arg( ap, double );
// allow any necessary cleanups to happen
va_end( ap );
}
A NULL pointer may also be a valid parameterMessiahAndrw wrote:With certain types of data (like pointers (even works with C*)), you can have a variable length of arguments without specifying how many there are by the last argument being 0 or NULL.
This doesn't work for certain types of input (e.g. ints) where 0 is a valid parameter!
If it's a user-space application, assign NULL to the beginning of kernel memory.Tomaka17 wrote:A NULL pointer may also be a valid parameterMessiahAndrw wrote:With certain types of data (like pointers (even works with C*)), you can have a variable length of arguments without specifying how many there are by the last argument being 0 or NULL.
This doesn't work for certain types of input (e.g. ints) where 0 is a valid parameter!
Awwwww... but then myMessiahAndrw wrote:If it's a user-space application, assign NULL to the beginning of kernel memory.
Code: Select all
if (ptr = (long*)malloc(sizeof(long) * array_size)) { /* ptr is valid for use */ }
From the C Standard.If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.