Function in C

Programming, for all ages and all languages.
Srowen
Member
Member
Posts: 60
Joined: Thu Feb 26, 2009 2:31 pm
Location: Genova, ITALY

Function in C

Post 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?
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: Function in C

Post by neon »

Search Google for "variable argument lists".
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
DeletedAccount
Member
Member
Posts: 566
Joined: Tue Jun 20, 2006 9:17 am

Re: Function in C

Post by DeletedAccount »

Hi ,
Of course its possible :) . search for variable arguments in C .


Regards
Shrek
User avatar
Creature
Member
Member
Posts: 548
Joined: Sat Dec 27, 2008 2:34 pm
Location: Belgium

Re: Function in C

Post by Creature »

The basic point is you use argument lists and something like

Code: Select all

void foo(int something, ...);
If I remember it correctly.
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Function in C

Post 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.
Every good solution is obvious once you've found it.
User avatar
steveklabnik
Member
Member
Posts: 72
Joined: Wed Jan 28, 2009 4:30 pm

Re: Function in C

Post by steveklabnik »

The actual term is variadic functions, not that 'variable argument list' wouldn't work.
Srowen
Member
Member
Posts: 60
Joined: Thu Feb 26, 2009 2:31 pm
Location: Genova, ITALY

Re: Function in C

Post by Srowen »

thanks to everyone...it is exactly what i needed...
User avatar
AndrewAPrice
Member
Member
Posts: 2299
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Re: Function in C

Post 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!
My OS is Perception.
User avatar
Tomaka17
Member
Member
Posts: 67
Joined: Thu Oct 02, 2008 8:20 am

Re: Function in C

Post 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 :P
MysteriOS
Currently working on: TCP/IP
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Function in C

Post by Combuster »

passsing pointers to pointers, anyone? :)
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
AndrewAPrice
Member
Member
Posts: 2299
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Re: Function in C

Post 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 :P
If it's a user-space application, assign NULL to the beginning of kernel memory.
My OS is Perception.
User avatar
Wilkie
Member
Member
Posts: 44
Joined: Tue Aug 26, 2008 10:02 pm
Location: Land of the Dead
Contact:

Re: Function in C

Post 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. :)
User avatar
AndrewAPrice
Member
Member
Posts: 2299
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Re: Function in C

Post 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.
My OS is Perception.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Function in C

Post 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. ;-)
Every good solution is obvious once you've found it.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Function in C

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply