Article View: comp.lang.c.moderated
Article #2169Re: Variable param for non-vararg subr. calls
From: Jens M Andreasen
Date: Mon, 04 Mar 1996 04:15
Date: Mon, 04 Mar 1996 04:15
149 lines
3759 bytes
3759 bytes
/*
Amazingly, my previous stunt did compile and run on quite
a few implementations but (ehrm) it must have shocked those
of you into good manners and welldefined behaviour ...
I have given up the idea and reverted to simplicity. I mean,
why make things more complicated than they are?
To have a set of functions grouped together seems to be a
neat idea though, and could come in handy from time to time,
Based on the fact that the programmer actually has to type
in the arguments, then their number is also known at compile
time and a switch can be used (alternatively a straight
#define.)
But selecting the proper level of information hiding becomes
highly academical without detailed knowledge of the project.
The following fragment offers maximum flexibility including
several ways of shooting oneself in the foot.
Adapt to personal taste :-)
// Jens M Andreasen
*/
/* stack_test_revisited (no stack!) ------------------- */
#include <stdio.h>
#define F_call_set1(a1,S) if(S.F1){S.error=0;\
S.F1(a1);}else{S.error=1;}
#define F_call_set2(a1,a2,S) if(S.F2){S.error=0;\
S.F2(a1,a2);}else{S.error=1;}
#define F_call_set3(a1,a2,a3,S) if(S.F3){S.error=0;\
S.F3(a1,a2,a3);}else{S.error=1;}
#define F_call_set4(a1,a2,a3,a4,S) if(S.F4){S.error=0;\
S.F4(a1,a2,a3,a4);}else{S.error=1;}
#define F_call_set(count,S) S.error=0;\
switch(count){\
\
case 1:S.F1(set.arg1);break;\
case 2:S.F2(set.arg1,set.arg2);break;\
\
case 4:S.F4(set.arg1,set.arg2,set.arg3,set.arg4);break;\
default:S.error=1;\
}
/* A set of related functions having common parameters
* Most probably they shouldn't be declared this openly.
*/
struct set_of_functions{
char *arg1;
char *arg2;
double arg3;
int arg4;
int(*F1)(char*);
void(*F2)(char*, char*);
void(*F3)(const char*, ...);
int(*F4)(const char*, ...);
int returnvalue; /* not used, but could be ... */
int error;
}set;
void MyFun(char* c,char* d){ /* causes a sideeffect */
static char *e = "\n Hey, look at this!!\n";
puts (c);
puts (d);
set.arg2 = e;
}
main(void){
char some_format[] = "\n Format string! %s %.1f, %#x\n";
char some_string[] = " A very dull string!";
float some_float = 1.00001;
char some_char = '2'; /* 0x32 */
int argcount;
set.F1 = puts;
set.F2 = MyFun;
set.F3 = NULL;
set.F4 = printf;
/* Let's for a moment just pretend that
* we only have two arguments, then:
*
* set.arg1 = " Pearls on a string\n";
* set.arg2 = " and a golden ring ...\n";
* F_call_set(2,set);
*
* Not too fuzzy? Else use a direct call:
*
* F_call_set2(some_string,some_string,set);
*
*/
/* Items can also be collected on the fly for later use */
set.arg1 = some_format;
set.arg2 = some_string;
set.arg3 = some_float;
set.arg4 = some_char;
puts(" Testrun of functions F1 .. F4 -------------");
argcount = 1;
do{
F_call_set(argcount,set);
if(!set.error)puts("\n - next ----");
}while(argcount++ < 4);
F_call_set2("\n That's all!"," -----------\n",set);
return 0 ;
}
/*
*
"And you may ask yourself: Where is that big house? And where is ... "
*
*/
Technical Details
Message-ID:
<4hdqp2$8sr@solutions.solon.com>Path:
arc1-ca.pugleaf.net!solon.com!not-for-mailReferences:
<199603012002.MAA04513@elf.bsdi.com> <4h9mjl$grs@solutions.solon.com> <4ha84e$js1@solutions.solon.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail From: Jens M Andreasen <jens-and@dsv.su.se> Newsgroups: comp.lang.c.moderated Subject: Re: Variable param for non-vararg subr. calls Date: 3 Mar 1996 22:15:30 -0600 Organization: Digital Solutions Sender: clc@solutions.solon.com Approved: clc@solutions.solon.com Message-ID: <4hdqp2$8sr@solutions.solon.com> References: <199603012002.MAA04513@elf.bsdi.com> <4h9mjl$grs@solutions.solon.com> <4ha84e$js1@solutions.solon.com> NNTP-Posting-Host: solutions.solon.com