optional arguments..

J

Jason

Hi there,

I'm new to C .. have a very basic question .. hope someone has the
patience to help!

I want to declare a function that has optional parameters and I want to
initialize the non supplied parameters .. how do I do this?

eg:

// this obviously isn't allowed .. but need to acheive something
similar
int function foo (int a=0, int b=0, int c=0, int d=0) {

// do something
return 0;
}

// I want to be able to call this function with any or all the
parameters

foo (2, 5,); // in this case a=2, b=5, c should be 0 and d should be 0


How do I accomplish this?


Thanks a bunch guys!
 
P

Peter Nilsson

Jason said:
Hi there,

I'm new to C .. have a very basic question .. hope someone has the
patience to help!

I want to declare a function that has optional parameters and I want to
initialize the non supplied parameters .. how do I do this?

You can't in C. You can mimic it to some extent (e.g. va_xxxx macros in
eg:

// this obviously isn't allowed .. but need to acheive something
similar
int function foo (int a=0, int b=0, int c=0, int d=0) {

// do something
return 0;
}

If you really want to do this, try C++.

If you need to use C only, then you should elaborate on what your
problem actually is, rather than state what you think is the solution
to an as yet unspecified problem.
 
J

Jason

Thanks Peter. To be honest I haven't programmed in ages and was
wondering if this was possible in C at all.

Cheers!
 
J

jacob navia

Jason said:
Hi there,

I'm new to C .. have a very basic question .. hope someone has the
patience to help!

I want to declare a function that has optional parameters and I want to
initialize the non supplied parameters .. how do I do this?

eg:

// this obviously isn't allowed .. but need to acheive something
similar
int function foo (int a=0, int b=0, int c=0, int d=0) {

// do something
return 0;
}

// I want to be able to call this function with any or all the
parameters

foo (2, 5,); // in this case a=2, b=5, c should be 0 and d should be 0


How do I accomplish this?


Thanks a bunch guys!
The lcc-win32 compiler implements this as an extension. The syntax is
the same as you wrote.

http://www.cs.virginia.edu/~lcc-win32
 
D

Default User

Jason said:
Thanks Peter. To be honest I haven't programmed in ages and was
wondering if this was possible in C at all.


Please review the information below.



Brian
 
P

Peter Shaggy Haywood

Groovy hepcat Jason was jivin' on 27 Apr 2006 18:28:25 -0700 in
comp.lang.c.
optional arguments..'s a cool scene! Dig it!
I'm new to C .. have a very basic question .. hope someone has the
patience to help!

No worries! We're very patient with people who are on-topic and
willing to learn.
I want to declare a function that has optional parameters and I want to
initialize the non supplied parameters .. how do I do this?

It wouldn't be too hard, using a variadic function. You'd need
another parameter, though, representing the number of variadic
arguments passed.
eg:

// this obviously isn't allowed .. but need to acheive something
similar
int function foo (int a=0, int b=0, int c=0, int d=0) {

// do something
return 0;
}

#include <stdarg.h>

int foo(size_t nargs, ...)
{
int a = 0, b = 0, c = 0, d = 0;
int *arr[4];
va_list ap;
size_t i;

arr[0] = &a;
arr[1] = &b;
arr[2] = &c;
arr[3] = &d;

va_start(ap, nargs);

for(i = 0; i < nargs; i++)
{
*arr = va_arg(ap, int);
}

va_end(ap);

/* Do something. */

return 0;
}

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top