473,396 Members | 1,827 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,396 software developers and data experts.

Semi-defined Function arguments???

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
Jun 6 '06 #1
2 1536

Ben wrote:
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.

Jun 6 '06 #2
Ben
Thanks! I'll check it out

Vladimir Oka wrote:
Ben wrote:
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.

Jun 6 '06 #3

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

Similar topics

2
by: Dennis M. Marks | last post by:
I am never sure of when a semi-colon is required in javascript. Is there a definite rule? -- Dennis M. Marks -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----...
7
by: jhomp ssens | last post by:
I would like to create a pulldown menu which is semi-transparent....that is, you can see the text and graphics behind it when it is pulled down. The effect I'm looking for can be seen at...
16
by: Kiuhnm | last post by:
Is there an elegant way to deal with semi-circular definitions? Semi-circular definition: A { B }; B { *A }; Circular reference: A { *B }; B { *A }; The problems arise when there are more...
2
by: David Scemama | last post by:
Hi, I'm looking for a way to display semi graphic characters in a multi line text control or in a rich text control. I've tried with all the characters of the extended ASCII table (code page...
2
by: Trond Michelsen | last post by:
Hi. I have a transparent PNG-image that I would like to display on top of the rest of the web page. I've already got this part working. But, I'd like the background (as in "the part of the image...
2
by: James Stroud | last post by:
Hello All, I am trying to create a semi-standalone with the vendor python on OS X 10.4 (python 2.3.5). I tried to include some packages with both --packages from the command and the 'packages'...
0
by: James Arnold | last post by:
I am trying to use a semi transparent PNG as the form background, allowing you to see through certain parts. The intention is similar to a skinnable form like launchy, with semi-transparent pixels...
9
by: JamesF1982 | last post by:
Hey everyone, My question is related to HTML, Javascript, CSS and ASP.NET but i think it belongs here! Upon an event i am trying to add a semi-transparent colour across the page so the...
3
by: nigel | last post by:
Hi, I'm using VBA to export data from a table direct to a CSV file DoCmd.TransferText acExportDelim, , "ExportTable", filePath this produced a file with COMMA separated values,...
3
by: Sylvain | last post by:
Hi, I'm playing with Google App Engine and during my tests it seems that there is a bug in cgi. parse_header function. If we upload a file with a semi-colon (i.e : "C:/my;file.jpg") :...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.