/* from SICP -- Exercise 4.21:
((lambda (n)
((lambda (fact)
(fact fact n))
(lambda (ft k)
(if (= k 1)
1
(* k (ft ft (- k 1)))))))
10)
*/
/* I want to do the same thing like that in C/C++. */
#include <stdio.h>
/* I find that I can't declare the function what I want, in standard
C/C++.
* My colleague try the following code. It's OK, but I find something
wrong.
*/
int f( int (*g)(), int x )
{
if (x==0)
return 1;
else
return x * (*g)(g, x-1);
}
int h(void)
{
return 2;
}
int main()
{
printf("%d\n", f(f,5)); /* This is what I want. */
/* This is not I want; but the complier didn't warning. */
printf("%d\n", f(h,5));
return 0;
}
==========
/* I find a way to achieve it indirectly, but I do not like it. */
#include <stdio.h>
typedef struct scm {
int (*fp)(struct scm, int);
} SCM;
int f(SCM g, int x)
{
if (x==0)
return 1;
else
return x * (g.fp)(g, x-1);
}
int main()
{
SCM F;
F.fp = f;
printf("%d\n", f(F,5)); /* It's ugly.*/
return 0;
}
/* I am sorry for my poor english. I hope you can understand what I
want.
* I just want to try wrinting "continuation" in C/C++.
*
* This is my question:
* Can I do the same thing like this in standard C/C++?
* Can I passing a funtion pointer to a FUNTION which point to THE
FUNTION?
*/ 5 1913
Lee Xuzhang said:
<Lithp thnipped> /* I want to do the same thing like that in C/C++. */
This is a Frequently-Asked Question, or FAQ. Steve Summit maintains a list
of answers to such questions. The relevant one for you can be found here:
<http://c-faq.com/decl/recurfuncp.html>
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Lee Xuzhang wrote: /* from SICP -- Exercise 4.21: ((lambda (n) ((lambda (fact) (fact fact n)) (lambda (ft k) (if (= k 1) 1 (* k (ft ft (- k 1))))))) 10) */
/* I want to do the same thing like that in C/C++. */
#include <stdio.h>
/* I find that I can't declare the function what I want, in standard C/C++. * My colleague try the following code. It's OK, but I find something wrong. */ int f( int (*g)(), int x ) { if (x==0) return 1; else return x * (*g)(g, x-1); }
int h(void) { return 2; }
/* try the following change */
typedef int(*fp)(void*, int);
int fun( fp x , int y)
{
if(y ==0 )
return 1;
else
return y * (*x)(x, y-1);
} int main() { printf("%d\n", f(f,5)); /* This is what I want. */
printf("%d\n", fun(fun,5)); /*work fine*/
/* This is not I want; but the complier didn't warning. */ printf("%d\n", f(h,5));
printf("%d\n", fun(h,5)); /*compiler will give
warning*/ return 0; }
==========
/* I find a way to achieve it indirectly, but I do not like it. */ #include <stdio.h>
typedef struct scm { int (*fp)(struct scm, int); } SCM;
int f(SCM g, int x) { if (x==0) return 1; else return x * (g.fp)(g, x-1); }
int main() { SCM F; F.fp = f; printf("%d\n", f(F,5)); /* It's ugly.*/
return 0; }
/* I am sorry for my poor english. I hope you can understand what I want. * I just want to try wrinting "continuation" in C/C++. * * This is my question: * Can I do the same thing like this in standard C/C++? * Can I passing a funtion pointer to a FUNTION which point to THE FUNTION? */
Lee Xuzhang wrote: int f( int (*g)(), int x )
It's ok. The first parameter g is type of the abstract type int (*) ().
Read the prototype of the library function signal through man signal.
H&S5 §9.2 provides two forms of the prototype of signal.
return x * (*g)(g, x-1);
How can two arguments be passed into the function? Function call and
prototype are mismatch.
--
lovecreatesbeauty
Thank you.
To Haider:
Your code is work fine in C, but it can't work in C++.
And, from FAQ 4.13:
"...void *'s are only guaranteed to hold object (i.e. data) pointers;
it is not portable to convert a function pointer to type void *. (On
some machines, function addresses can be very large, bigger than any
data pointers.) ..."
(Thanks for Richard Heathfield)
Is "typedef int(*fp)(void*, int); " still right?
Maybe I can't quite do it directly.
( http://c-faq.com/decl/recurfuncp.html)
Thanks for all.
On 14 Jun 2006 05:10:49 -0700, "Lee Xuzhang" <li*******@gmail.com> wrote: Your code is work fine in C, but it can't work in C++.
You'll have to ask in a C++ group.
--
#include <standard.disclaimer>
_
Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: harry |
last post by:
I have 2 multi-dim arrays
double subTotals = null;
String rowTitles = null;
I want to pass them to a function that initialises & populates them like
so -
loadData( rowTitles, subTotals);
|
by: jr |
last post by:
Sorry for this very dumb question, but I've clearly got a long way to go!
Can someone please help me pass an array into a function. Here's a starting
point.
void TheMainFunc()
{
// Body of...
|
by: |
last post by:
Say we have the following code defining TMyMsgHandler and TMyClass
typedef void (*TOnMsgReceive) (TMyMessage Msg);
class TMyMsgHandler
{
public:
TMyMsgHandler();
virtual ~TMyMsgHandler();...
|
by: Pushkar Pradhan |
last post by:
I want a function to execute another function, which I pass to it,
sometimes it may be mm_6r6c_6r6c, mm_2r2c_2r2c, ... etc. thus this style.
double exec_basecase(void (*func)(double *a, double...
|
by: Albert Albani |
last post by:
Hello
I have a problem that I cannot seem to solve
I have a c funtion in a DLL that basically looks like
func_c (some_struct* s) {...}
some_struct is defined like so:
struct some_struct {
|
by: Ivan Liu |
last post by:
Hi,
I'd like to ask if passing an object as an pointer into a function
evokes the copy constructor.
Ivan
|
by: steven_orocos |
last post by:
Hi,
I'm tryin to pass a funtion as an argument in another funtion.
This code works
----------------------------------
typedef void (*func)(int);
void test1(int a){cout<<"1";}
void...
|
by: Amit_Basnak |
last post by:
Dear friends
I have to pass the objec of a class which is a part of afunction in
thefunction call.
my code looks like this now
#include <iostream.h>
#include <waspc/common.h>
#include...
|
by: masso600 |
last post by:
char word;
in = fopen("test.txt", "r");
while(fscanf(in,"%s",&word)!=EOF)
{
/* Print all words */
/* printf("%s\n",&word); */
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
| |