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

Function pointers

MrPickle
100 100+
Is it possible to have a function pointer without knowing the arguments of the function?

All tutorials I have read have declared the function pointer like so:
int (*func)(int, float);

could that just be declared as
int (*func);

or would the compiler assume that there is no arguments?
Mar 1 '09 #1
11 3977
weaknessforcats
9,208 Expert Mod 8TB
If you have a function pointer named fp and you use it this way:

Expand|Select|Wrap|Line Numbers
  1. int x = fp(3,4);
  2. float y = fp(1.0,2.0);
which is correct? Does the function pointed at by fp have two int arguments returning an int or two double arguments returning a float?

Unless the arguments and return type are specified in the function pointer declaration, the compiler has no way to determine of you are calling function correctly.
Mar 1 '09 #2
MrPickle
100 100+
So you have to define the arguments?

Is there anyway around this, like using void pointers?
Mar 1 '09 #3
JosAH
11,448 Expert 8TB
@MrPickle
Yes, you have to define the type(s) of the argument(s) and the type of the return value of the function; both form the 'signature' of a function. A compiler needs to know what the signature of a function (pointer) is in order to be able to use it. You can lie to the compiler of course but you're on your own then.

No, you can't just use void pointers if the actual function doesn't expect void pointers as the types of its arguments.

kind regards,

Jos
Mar 2 '09 #4
donbock
2,426 Expert 2GB
I assume you are asking the question because you have a function pointer variable and you want it to point to different kinds of functions (ie, with differing prototypes) based on some criteria. You're confident that you will know which kind of function is pointed to (ie, what arguments to pass) when it is time to call the function being pointed at.

1. I recommend you use several function pointer variables -- one for each logically different kind of function.

2. If you insist on a single function pointer then you could look into declaring a union of function pointers.

3. If this is some kind of C riddle, then perhaps the answer is to declare the function pointer with ellipsis and then cast to and from the actual prototype. This approach is definitely NOT recommended if you care whether your software works or not.

4. Keep in mind that function pointers and data pointers are very different things. You cannot reliably convert from one to the other -- regardless of whether you can find a way to suppress compiler warnings. A corollary of this is that you should never assign NULL to a function pointer. If you need a null function pointer value then construct it for yourself:
Expand|Select|Wrap|Line Numbers
  1. #define FNULL 0L
Mar 2 '09 #5
MrPickle
100 100+
Ok thank you, I am using C++ by the way.

Could you use a template of some sort, to give the function pointer some dynamics?
Mar 2 '09 #6
weaknessforcats
9,208 Expert Mod 8TB
Look at the implementation of the C++ STL. You have generator functions (no arguments), unary functions (one argument) and binary functions (two arguments) and that's all.

If you need 10 arguments you need to create ano object with 8 of the arguments installed as member data and then the operator() can have the final 2arguments, which it can combine with the 8 data members to call your 10 argument function. The STL implements the binder1st and binder2nd classes to help you out.

There is, however, no simple general purpose solution. If you use a void* for your function pointer, you still have to typecast it to the correct function pointer which means you still have to specify the arguments and return type at some point.

When Microsoft implemented multi-threading and allowed you to create threads they published a thing called a THREADPROC:

Expand|Select|Wrap|Line Numbers
  1. DWORD WINAPI ThreadProc(LPVOID);
This meant that if your function is to become thread it must have one LPVOID argument and return a DWORD. Nothing else will work.
Mar 2 '09 #7
MrPickle
100 100+
Thank you, I will take a look at the STL functions and then play a little.
Mar 2 '09 #8
MrPickle
100 100+
Okay, so I've took a look at the STL functions but I can't see where the function pointer define's the arguments.

I'll take for_each as an example:
Expand|Select|Wrap|Line Numbers
  1. template <class In, class Op> Op for_each(In first, In last, Op f)
  2. {
  3.     while(first != last) f(*first++);
  4.     return f;
  5. }
I see no argument declaration for the function pointer, nor do I see a pointer declaration? (I copied the code from "The C++ Programming Language" by Bjarne Stoustrup so I'm 99% sure it's correct.
Mar 3 '09 #9
scruggsy
147 100+
@MrPickle
You see nothing about function pointers there because foreach is a template function. You can instantiate it with any type for f so long as calling f(*first++) is a legal operation. This means f must be a function taking one argument or an object of a class with an overloaded operator()() method taking one argument. Moreover because first is of type In, f must take an argument convertible to whatever type In is when the template is instantiated. (Similarly, first and last must be of a primitive or user-defined type which supports the *, !=, and ++ operators).

You may want to look into templates along with the STL and its notion of functors and predicates; you're likely to make heavy use of them if you do much work in C++.
Mar 4 '09 #10
weaknessforcats
9,208 Expert Mod 8TB
You may want to look into templates along with the STL and its notion of functors and predicates; you're likely to make heavy use of them if you do much work in C++.
OP: A functor is a class implementing the function operator. That is, it implements operator() as a member function. That operator() can have zero, one or two arguments only.

Those are the generator, unary and binary functions.

If your unary function returns true or false, then it is called a predicate.
If your binary function returns true or false, then it is called a binary predicate.

You have to be clear on this since the STL documentation uses these terms, often without explanation. That is, when you read binary predicate your are to understand that you need to provide the address of a function that takes two arguments and returns a bool.
Mar 4 '09 #11
MrPickle
100 100+
I have learnt of something in the boost libraries, boost::bind, it fits this purpose beautifully. Just letting everybody know of it for in the future.

http://www.boost.org/doc/libs/1_38_0...bind/bind.html

Thanks all.
Mar 5 '09 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Markus Dehmann | last post by:
I have a class "Data" and I store Data pointers in an STL set. But I have millions of inserts and many more lookups, and my profiler found that they cost a lot of runtime. Therefore, I want to...
89
by: Sweety | last post by:
hi, Is main function address is 657. its show in all compiler. try it & say why? bye,
3
by: Dennis Chang | last post by:
Hi all, I was reading about function pointers and came across something which intrigued me. K&R2 calls qsort (pg.119) within main as so: qsort( (void **) lineptr, 0, nlines-1, (int (*) (void...
41
by: Alexei A. Frounze | last post by:
Seems like, to make sure that a pointer doesn't point to an object/function, NULL (or simply 0) is good enough for both kind of pointers, data pointers and function pointers as per 6.3.2.3: 3 An...
12
by: Bill Pursell | last post by:
The following code generates a compiler warning when compiled with gcc -pedantic: typedef (*FUNC)(int); FUNC f; void * get_f(void) { return &f;
57
by: Robert Seacord | last post by:
i am trying to print the address of a function without getting a compiler warning (i am compiling with gcc with alot of flags). if i try this: printf("%p", f); i get: warning: format %p...
54
by: John | last post by:
Is the following program print the address of the function? void hello() { printf("hello\n"); } void main() { printf("hello function=%d\n", hello); }
4
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
32
by: copx | last post by:
Why doesn't the C standard include generic function pointers? I use function pointers a lot and the lack of generic ones is not so cool. There is a common compiler extension (supported by GCC...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.