468,268 Members | 1,511 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,268 developers. It's quick & easy.

Default values for arguments

Hi,

Is it possible to define default values for arguments that are not
passed when the function call is made ?

I tried it , but it showed linking errors. Any other ideas/help ?

Thx in advans,
Karthik Balaguru

Sep 2 '07 #1
8 1257
karthikbalaguru wrote:
Hi,

Is it possible to define default values for arguments that are not
passed when the function call is made ?
Not in C, you must be thinking of C++.

--
Ian Collins.
Sep 2 '07 #2
karthikbalaguru wrote:
Hi,

Is it possible to define default values for arguments that are not
passed when the function call is made ?

I tried it , but it showed linking errors. Any other ideas/help ?

Thx in advans,
Karthik Balaguru
The lcc-win32 compiler features this as an extension.

It is not possible in standard C.
Sep 2 '07 #3
karthikbalaguru wrote:
Hi,

Is it possible to define default values for arguments that are not
passed when the function call is made ?

I tried it , but it showed linking errors. Any other ideas/help ?
Yes. Just code in the default value either as a const qualified variable or
with the help of the define preprocessor directive.

Sep 2 '07 #4
On Sep 2, 6:46 am, karthikbalaguru <karthikbalagur...@gmail.com>
wrote:
Is it possible to define default values for arguments that are not
passed when the function call is made ?

I tried it , but it showed linking errors. Any other ideas/help ?

Unless the function is variadic and has a declaration of the form
T f(T, ...); or the ill-advised and old fashioned form
T f(); you must pass all arguments.

A common idiom is to pass flags in the required arguments
which indicate which of the optional arguments are being
passed (eg open() in unix). For example, if you have a
function that constructs an empty list that takes 2
optional arguments, an initial size and a memory allocation
function, you could do:

struct list * new_list( int flags, ...);

and make calls to it like:

struct list * A;
A = new_list( 0 );
A = new_list( LIST_SIZE, 1024 );
A = new_list( LIST_SIZE | LIST_MALLOC, 1024, malloc );

In the definition of new_list(), you check the flags
and set values appropriately.

Sep 2 '07 #5
santosh wrote:
karthikbalaguru wrote:
>Hi,

Is it possible to define default values for arguments that are not
passed when the function call is made ?

I tried it , but it showed linking errors. Any other ideas/help ?

Yes. Just code in the default value either as a const qualified
variable or with the help of the define preprocessor directive.
Can you give an example of how to do it.
I can't think of a way to specify default values for function argument,
so that you don't have to specify them at the call site.

Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/
Sep 2 '07 #6
On Sun, 02 Sep 2007 13:24:54 +0200, jacob navia wrote:
karthikbalaguru wrote:
>Hi,

Is it possible to define default values for arguments that are not
passed when the function call is made ?

I tried it , but it showed linking errors. Any other ideas/help ?

Thx in advans,
Karthik Balaguru

The lcc-win32 compiler features this as an extension.
So what? This group is about C, the OP could not be using
lcc-win32, or even Windows at all. If the OP was interested to one
particular compiler, (s)he'd have asked on a group about that
compiler. (Actually there are newbies who happen to make questions
about one compiler, but usually they *say* what that compiler is,
and Karthik didn't.)
<flame>IOW: go spamming elsewhere</flame>
It is not possible in standard C.
Right.
--
Army1987 (Replace "NOSPAM" with "email")
No-one ever won a game by resigning. -- S. Tartakower

Sep 3 '07 #7
Ian Collins <ia******@hotmail.comwrote:
# karthikbalaguru wrote:
# Hi,
# >
# Is it possible to define default values for arguments that are not
# passed when the function call is made ?
# >
# Not in C, you must be thinking of C++.

You can sometimes simulate this with var-args, but remember var-args
doesn't tell you what the actual argument list is. You need some
other protocol to know how long the argument list is and what the
parameter types are.
--
SM Ryan http://www.rawbw.com/~wyrmwif/
Wow. A sailboat.
Sep 3 '07 #8
Bart van Ingen Schenau wrote:
santosh wrote:
>karthikbalaguru wrote:
>>Hi,

Is it possible to define default values for arguments that are not
passed when the function call is made ?

I tried it , but it showed linking errors. Any other ideas/help ?
Yes. Just code in the default value either as a const qualified
variable or with the help of the define preprocessor directive.

Can you give an example of how to do it.
I can't think of a way to specify default values for function argument,
so that you don't have to specify them at the call site.

Bart v Ingen Schenau
Somethin' along these lines:
extern int foo(int, int);
#define foo(a) foo((a), 5)
int bar(int a, int b)
{
(foo)(a, b);
return foo(a);
}
Note that the function name is parenthesized. Can only hide one
parameter though.

-- Ark
Sep 4 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by earl | last post: by
18 posts views Thread by Dan Cernat | last post: by
44 posts views Thread by gregory.petrosyan | last post: by
10 posts views Thread by Alan G Isaac | last post: by
7 posts views Thread by George Sakkis | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.