Solved

Pure C - function with optional arguments only

Posted on 2003-11-03
6
515 Views
Last Modified: 2010-04-15
Hi,

I want to declare a function, which has no required parameters, but may have optional, and do it in pure C.

It's simple for functions having at least one parameter:
   void my_func( char *sFirst, ... );
and then accessing the optional using arg_list.

I want to define something like
   void my_func( ... );
but it's not valid :(


I'll appreciate your help.
0
Comment
Question by:migoEX
6 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9669764
man 3 getopt

something like
while ((ch = getopt(argc, argv, "a:dhf:l:m:np:rs:v")) != EOF)
should serve your purpose

the above line of code is from syslogd source code
0
 
LVL 6

Assisted Solution

by:Ajar
Ajar earned 30 total points
ID: 9669871
hey you can always declare a pointer to a function without no args e.g


void ( * function)();

void  a_function_implementation(int  param1);
{
   // The param may not have been passed so the question is how
  // do you intend to find out wether a param has been passed or not;
  puts("INSIDE THE FUNCITON ");
}

int main(int argc,char* argv[])
{
    function = a_function_implementation;
    function();
}
0
 
LVL 5

Author Comment

by:migoEX
ID: 9670048
There is 1 problem left:

I want this function to be exported, and used by "external" functions. If in my H file I declare the function without parameters, the caller won't be able to compile a code, which is passing parameter. And vise versa - if I define it with parameter, a call without passing parameters won't compile.

PS: I'm not sure it's possible :)  but may be I'm missing some solution
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 45

Expert Comment

by:sunnycoder
ID: 9670120
Even I would be interested in the solution if this was possible ...
0
 
LVL 5

Accepted Solution

by:
mtmike earned 170 total points
ID: 9670425
You can declare the function with an empty argument list, eg

void myfunc();

In C (not C++) this functions takes any number of arguments. The above means "arguments are still unspecified". You can specify the arguments later on when defining the function.

The above is also the reason that functions without arguments should be defined as

void myfunc(void);
0
 
LVL 5

Author Comment

by:migoEX
ID: 9670508
Thanks, mtmike!

That's exactly what I was looking for :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now