473,382 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

flexible function parameter

Hi all,

I've got a question according the design
of a parameterlist in C. I am going to have
a function taking two parameters.

One is supposed to be used as a flag, which
determines the type of data is needed, which
is passed to the function by the second parameter.
The type of this parameters differs depending on
the flag. E.g.

void doSomething ( int flag, <seeBelow> value)
{
...
switch flag
{
case 1: // I need value to be a double
...
case 2: // Value shoud be an int
...
}
...
}
Since the number of different types is finite,
I wonder what is the best bet to pass the 2nd Parameter
a) Build a struct, containing all possibilities
of which only the needed are set
b) Use a void pointer
c) Something else

Thanks
Michael

Nov 14 '05 #1
4 2494

"Michael" <mi****@gmx.net> wrote in message
news:c6**********@rzcomm2.rz.tu-bs.de...
Hi all,

I've got a question according the design
of a parameterlist in C. I am going to have
a function taking two parameters.

One is supposed to be used as a flag, which
determines the type of data is needed, which
is passed to the function by the second parameter.
The type of this parameters differs depending on
the flag. E.g.

void doSomething ( int flag, <seeBelow> value)
{
...
switch flag
{
case 1: // I need value to be a double
...
case 2: // Value shoud be an int
...
}
...
}
Since the number of different types is finite,
I wonder what is the best bet to pass the 2nd Parameter
a) Build a struct, containing all possibilities
of which only the needed are set
b) Use a void pointer
c) Something else

Thanks
Michael


c) something else, a union.
Allan
Nov 14 '05 #2
Allan Bruce wrote:

"Michael" <mi****@gmx.net> wrote in message
news:c6**********@rzcomm2.rz.tu-bs.de...
Hi all,

I've got a question according the design
of a parameterlist in C. I am going to have
a function taking two parameters.

One is supposed to be used as a flag, which
determines the type of data is needed, which
is passed to the function by the second parameter.
The type of this parameters differs depending on
the flag. E.g.

void doSomething ( int flag, <seeBelow> value)
{
...
switch flag
{
case 1: // I need value to be a double
...
case 2: // Value shoud be an int
...
}
...
}
Since the number of different types is finite,
I wonder what is the best bet to pass the 2nd Parameter
a) Build a struct, containing all possibilities
of which only the needed are set
b) Use a void pointer
c) Something else

Thanks
Michael


c) something else, a union.


c2) Something else, <stdarg.h>. While you're about it,
put parentheses around the `switch' expression.

--
Er*********@sun.com
Nov 14 '05 #3


Michael wrote:
Hi all,

I've got a question according the design
of a parameterlist in C. I am going to have
a function taking two parameters.

One is supposed to be used as a flag, which
determines the type of data is needed, which
is passed to the function by the second parameter.
The type of this parameters differs depending on
the flag. E.g.

void doSomething ( int flag, <seeBelow> value)
{
...
switch flag
{
case 1: // I need value to be a double
...
case 2: // Value shoud be an int
...
}
...
}
Since the number of different types is finite,
I wonder what is the best bet to pass the 2nd Parameter
a) Build a struct, containing all possibilities
of which only the needed are set
b) Use a void pointer
c) Something else


infinity of possiblities is a problem with all:
a) defining a struct(union) can be done but you have to
define all possiblities.
b) You can use void pointer but again your possibilities
are limited.

Of these two I would favor the second (void *). However
to give more flexible why not use the variable argument
macros.

/* The program below illustrates passing a variable
* arguments using the following macros:
* va_start va_arg va_end
* va_list
*/
#include <stdio.h>
#include <stdarg.h>

typedef enum {CHARPOINTER,DOUBLE,INTEGER,INTSUM} type;

void functest( type flag, ... );

int main(void)
{
functest(CHARPOINTER,"This is a test");
functest(DOUBLE, 12.20);
functest(INTEGER, 23);
functest(INTSUM,23,23);
functest(44,29);
return 0;
}

/* Testing variable arguments */
void functest( type flag, ... )
{
va_list marker;

va_start( marker, flag ); /* Initialize variable arguments. */
switch(flag)
{
case CHARPOINTER: printf("The argument is a char *\n"
"pointing to string \"%s\"\n\n",
va_arg(marker,char *));
break;
case DOUBLE: printf("The argument is a double\n"
"of value %f\n\n",
va_arg(marker,double));
break;
case INTEGER: printf("The argument is a type int\n"
"of value %d\n\n",
va_arg(marker,int));
break;
case INTSUM: {
int i1,i2;
i1 = va_arg(marker,int);
i2 = va_arg(marker,int);
printf("The arguments call for a"
"sumation.\n"
"%d + %d = %d\n\n",i1,i2,i1+i2);
}
break;
default: puts("The agrument is incorrectly specified");
}
va_end( marker ); /* Reset variable arguments. */
return;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #4
Thanks anyone for reply.

I agree, in case of defining a struct or union, I
have to think of all possibilties while definig the
struct and the function.
I guess I'm blind because the lack of experience, but
I don't see the advantage of the variable argument.
In both implementations (void* and var. arguments)
I need to know all possibilities when I write the body
of the function, don't I?

Regards,
Michael

and thanks again for the code you posted, since I am
not familar with the var arg-list it helps a lot


infinity of possiblities is a problem with all:
a) defining a struct(union) can be done but you have to
define all possiblities.
b) You can use void pointer but again your possibilities
are limited.

Of these two I would favor the second (void *). However
to give more flexible why not use the variable argument
macros.

[...]


Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: BoĆĄtjan Jerko | last post by:
Is there a way to pass list with flexible length to C extension? I want to pass flexible length list of floats to method and just can't get info if it is possible and how to do it. Thanks, B.
9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
10
by: Adam Warner | last post by:
Hi all, With this structure that records the length of an array of pointers as its first member: struct array { ptrdiff_t length; void *ptr; };
2
by: Christopher Benson-Manica | last post by:
Is the following program conforming under C99? #include <stdio.h> typedef struct foo { int bar; int baz; } foo; foo foos={
4
by: Vish | last post by:
Hi all, I am having a build error in one of the overloaded functions in my class. The function takes either a string as a parameter or a type referenced in another dll as a parameter. My class...
8
by: ulyses | last post by:
I'm trying to put pointer to flexible array of structures in other structure. I want to have pointer to array of pixels in screen structure. Here is mine code, but I think it isn't quite all right:...
16
by: hzmonte | last post by:
Correct me if I am wrong, declaring formal parameters of functions as const, if they should not be/is not changed, has 2 benefits; 1. It tells the program that calls this function that the...
4
by: Tony Lownds | last post by:
(Note: PEPs in the 3xxx number range are intended for Python 3000) PEP: 3107 Title: Function Annotations Version: $Revision: 53169 $ Last-Modified: $Date: 2006-12-27 20:59:16 -0800 (Wed, 27 Dec...
40
by: Angus | last post by:
Hello I am writing a library which will write data to a user defined callback function. The function the user of my library will supply is: int (*callbackfunction)(const char*); In my...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.