Page 1 of 2
Function in C
Posted: Thu Mar 05, 2009 8:48 am
by Srowen
it is possible in the C language create a function that doesn't know exactly the number of parametres?
for example, if i want to create a funcion similar to printf:
Code: Select all
void prinft(char *string1, char *string2, );
and so on...i hope that you understand what i want to say...any ideas on it?
Re: Function in C
Posted: Thu Mar 05, 2009 9:02 am
by neon
Search Google for "variable argument lists".
Re: Function in C
Posted: Thu Mar 05, 2009 9:04 am
by DeletedAccount
Hi ,
Of course its possible
. search for variable arguments in C .
Regards
Shrek
Re: Function in C
Posted: Thu Mar 05, 2009 9:15 am
by Creature
The basic point is you use argument lists and something like
If I remember it correctly.
Re: Function in C
Posted: Thu Mar 05, 2009 9:22 am
by Solar
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 );
}
The problem is that you have to know the
type of the parameters to get them off the stack correctly, and you have to know when to stop, because one va_arg() too many and you're dead. Well, your program is...
Depending on what your function does, the type may be part of the function API (e.g., always 'int' type, with EOF ending the list), or determined by the
known parameters (like the format string in printf() telling it how many items of which type to expect).
A va_list object may even be duplicated using va_copy( dest, src ), but that's tricky because you have to call va_end() properly, and seldom necessary anyway.
HTH.
Re: Function in C
Posted: Thu Mar 05, 2009 3:27 pm
by steveklabnik
The actual term is
variadic functions, not that 'variable argument list' wouldn't work.
Re: Function in C
Posted: Fri Mar 06, 2009 9:11 am
by Srowen
thanks to everyone...it is exactly what i needed...
Re: Function in C
Posted: Fri Mar 06, 2009 6:05 pm
by AndrewAPrice
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!
Re: Function in C
Posted: Sat Mar 07, 2009 4:31 am
by Tomaka17
MessiahAndrw 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!
A NULL pointer may also be a valid parameter
Re: Function in C
Posted: Sat Mar 07, 2009 8:16 am
by Combuster
passsing pointers to pointers, anyone?
Re: Function in C
Posted: Sun Mar 08, 2009 2:29 am
by AndrewAPrice
Tomaka17 wrote:MessiahAndrw 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!
A NULL pointer may also be a valid parameter
If it's a user-space application, assign NULL to the beginning of kernel memory.
Re: Function in C
Posted: Thu Mar 12, 2009 4:03 pm
by Wilkie
MessiahAndrw wrote:If it's a user-space application, assign NULL to the beginning of kernel memory.
Awwwww... but then my
Code: Select all
if (ptr = (long*)malloc(sizeof(long) * array_size)) { /* ptr is valid for use */ }
won't wooooooork.
Re: Function in C
Posted: Fri Mar 13, 2009 12:05 am
by AndrewAPrice
Then make NULL 0, make 0 the entry point for applications, and in most cases it'll be the very beginning of crt0 in the compiler's library. There could be specific times when it won't work, but for most userspace applications I don't see a reasonable scenario where it wouldn't work.
Re: Function in C
Posted: Fri Mar 13, 2009 2:19 am
by Solar
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.
From the C Standard.
Re: Function in C
Posted: Fri Mar 13, 2009 4:38 am
by Combuster
that's why you should compare to NULL instead of comparing to 0. On machines without MMU you'll see often enough that RAM starts at 0 and the program is in ROM somewhere else. And then your code must still be able to work.