Semi-defined Function arguments???

B

Ben

Hi,

I have simplified the situation a lot so hopefully this still makes sense...

Say I have a number of functions that work on any individual element of an array. I want to create a function "doOnAll" that
will call ANY of those functions on EVERY element of the array.

e.g.
void doOnAll (int x) {
for (i=0;i<n;i++) {
if (x==0) {
returnValue(i);
} else if (x==1) {
returnIndex(i);
} else if (x==2) {
returnCost(i);
}
}
}

No problem, but say each of the singular functions had different types of arguments instead of all being 'i' as they are above.
I could just pass these on to doOnAll if it's arguments were undefined:

void doOnAll () {
}

But then how would it know which function to call without the variable 'x'?

Can you 'half' specify a functions arguments, say (int x, *)?
Does anyone have any other ideas how to workaround this problem?

Note it may not be obvious why I need to do this but it's related to the equivalent of the 'for' line below in my program
actually being about 100 lines of code, and the structure being a four-dimensional array.


cheers,

Ben C
 
V

Vladimir Oka

Ben said:
Hi,

I have simplified the situation a lot so hopefully this still makes sense...

Say I have a number of functions that work on any individual element of an array. I want to create a function "doOnAll" that
will call ANY of those functions on EVERY element of the array.

e.g.
void doOnAll (int x) {
for (i=0;i<n;i++) {
if (x==0) {
returnValue(i);
} else if (x==1) {
returnIndex(i);
} else if (x==2) {
returnCost(i);
}
}
}

No problem, but say each of the singular functions had different types of arguments instead of all being 'i' as they are above.
I could just pass these on to doOnAll if it's arguments were undefined:

void doOnAll () {
}

But then how would it know which function to call without the variable 'x'?

Can you 'half' specify a functions arguments, say (int x, *)?
Does anyone have any other ideas how to workaround this problem?

Note it may not be obvious why I need to do this but it's related to the equivalent of the 'for' line below in my program
actually being about 100 lines of code, and the structure being a four-dimensional array.

You can make your function variadic. Look it up in your
textbook/manual.

Alternatively, you could try passing a [pointer to a] structure that
contains an element specifying the operation required (`x` above), and
the rest being data to work on. If data is different for different
operations you can use a union inside the structure, and use whatever
is indicated by the operation member of the structure.

There are obviously variations of the above.
 
B

Ben

Thanks! I'll check it out

Vladimir said:
Ben said:
Hi,

I have simplified the situation a lot so hopefully this still makes sense...

Say I have a number of functions that work on any individual element of an array. I want to create a function "doOnAll" that
will call ANY of those functions on EVERY element of the array.

e.g.
void doOnAll (int x) {
for (i=0;i<n;i++) {
if (x==0) {
returnValue(i);
} else if (x==1) {
returnIndex(i);
} else if (x==2) {
returnCost(i);
}
}
}

No problem, but say each of the singular functions had different types of arguments instead of all being 'i' as they are above.
I could just pass these on to doOnAll if it's arguments were undefined:

void doOnAll () {
}

But then how would it know which function to call without the variable 'x'?

Can you 'half' specify a functions arguments, say (int x, *)?
Does anyone have any other ideas how to workaround this problem?

Note it may not be obvious why I need to do this but it's related to the equivalent of the 'for' line below in my program
actually being about 100 lines of code, and the structure being a four-dimensional array.

You can make your function variadic. Look it up in your
textbook/manual.

Alternatively, you could try passing a [pointer to a] structure that
contains an element specifying the operation required (`x` above), and
the rest being data to work on. If data is different for different
operations you can use a union inside the structure, and use whatever
is indicated by the operation member of the structure.

There are obviously variations of the above.
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top