I am practicing function pointers. Here's what I am doing: passing a
function pointer to another function. It all goes well except for the
two warnings I get:
(1) 'ConsumeFnPointer' : too many actual parameters
(2) declared formal parameter list different from definition
I am curious as to why I get those warnings. Can we not pass variable
arguments to the right of the function pointer argument? Has it got
something to do with the calling sequence (__stdcall, ___pascal etc.)?
Here's the code:
#include <stdio.h>
/*float Add(float, float);
float Subtract(float, float);
float Multiply(float, float);
float Divide(float, float);
float (*ptr)(float, float);
*/
int DoSomething(int, int);
int ConsumeFnPointer(int (*ptr1)(int, int));
int main()
{
//ptr=&Add;
//printf("%f",ptr(3,9));
printf("%d", ConsumeFnPointer(&DoSomething,12,4));
}
/*float Add(float a, float b) { return a+b;}
float Subtract(float a, float b){ return a-b;}
float Multiply(float a, float b){ return a*b;}
float Divide(float a, float b){ return a/b;}
*/
int ConsumeFnPointer(int (*ptr1)(int a, int b), int a, int b)
{
return (*ptr1)(a,b);
}
int DoSomething(int a, int b)
{
return 1+a-b;
} 6 1210
"Sathyaish" <Vi****************@yahoo.com> wrote in message
news:7b**************************@posting.google.c om... I am practicing function pointers. Here's what I am doing: passing a function pointer to another function. It all goes well except for the two warnings I get:
(1) 'ConsumeFnPointer' : too many actual parameters (2) declared formal parameter list different from definition
I am curious as to why I get those warnings. Can we not pass variable arguments to the right of the function pointer argument? Has it got something to do with the calling sequence (__stdcall, ___pascal etc.)?
No.
int ConsumeFnPointer(int (*ptr1)(int, int));
.... printf("%d", ConsumeFnPointer(&DoSomething,12,4));
.... int ConsumeFnPointer(int (*ptr1)(int a, int b), int a, int b)
These three argument lists do not match.
S
--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Aaron Sorkin
"Sathyaish" <Vi****************@yahoo.com> wrote in message news:7b**************************@posting.google.c om... I am practicing function pointers. Here's what I am doing: passing a function pointer to another function. It all goes well except for the two warnings I get:
(1) 'ConsumeFnPointer' : too many actual parameters (2) declared formal parameter list different from definition
I am curious as to why I get those warnings. Can we not pass variable arguments to the right of the function pointer argument?
You can pass arguments in any order you may like. See below.
Has it got something to do with the calling sequence (__stdcall, ___pascal etc.)?
Here's the code:
#include <stdio.h>
[..] int DoSomething(int, int); int ConsumeFnPointer(int (*ptr1)(int, int));
ConsumeFnPointer() takes only one argument and it's type is: pointer to a
function accepting two int paramemters and returning an int.... int main() {
[..] printf("%d", ConsumeFnPointer(&DoSomething,12,4));
... whereas you are passing three arguments to the ConsumeFnPointer().
You should do like this:
printf("%d", ConsumeFnPointer(DoSomething));
Note that you do not need to use the address-of operator here, since a
function-designator, unless when it the operand of sizeof operator or the
address-of operator, is converted to a "pointer to function".
It is also worth noting that applying the sizeof operator to a
function-designator violates the constraints of the sizeof operator. }
/*float Add(float a, float b) { return a+b;} float Subtract(float a, float b){ return a-b;} float Multiply(float a, float b){ return a*b;} float Divide(float a, float b){ return a/b;} */
int ConsumeFnPointer(int (*ptr1)(int a, int b), int a, int b)
Tut..tut! This does not match with the prototype. It should be:
int
ConsumeFnPointer( int (*ptr1)(int a, int b) )
{
return ptr ( a, b ); /* DoSomething(a,b) would be called */
}
{ return (*ptr1)(a,b); }
int DoSomething(int a, int b) { return 1+a-b; }
--
Vijay Kumar R Zanvar
C FAQs - http://www.geocities.com/vijoeyz/faq/
I noticed immediately after making the post. Thanks for the awakening. :-)
"Vijay Kumar R Zanvar" <vi*****@globaledgesoft.com> writes: [...] It should be:
int ConsumeFnPointer( int (*ptr1)(int a, int b) ) { return ptr ( a, b ); /* DoSomething(a,b) would be called */ }
Unless `a' and `b' are available as file scope objects (which wasn't the
case in the OP's code), this is invalid, as `a' and `b' are undeclared.
Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
"Martin Dickopp" <ex****************@zero-based.org> wrote in message news:cu*************@zero-based.org... "Vijay Kumar R Zanvar" <vi*****@globaledgesoft.com> writes:
[...] It should be:
int ConsumeFnPointer( int (*ptr1)(int a, int b) ) { return ptr ( a, b ); /* DoSomething(a,b) would be called */ } Unless `a' and `b' are available as file scope objects (which wasn't the case in the OP's code), this is invalid, as `a' and `b' are undeclared.
Martin
I missed it! You're correct. Thanks for pointing out my mistake. -- ,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =. / ,- ) http://www.zero-based.org/ ((_/)o o(\_)) \ `-' `-'(. .)`-' `-. Debian, a variant of the GNU operating system. \_/
On Thu, 10 Jun 2004, Sathyaish wrote: I am practicing function pointers. Here's what I am doing: passing a function pointer to another function. It all goes well except for the two warnings I get:
(1) 'ConsumeFnPointer' : too many actual parameters (2) declared formal parameter list different from definition
I am curious as to why I get those warnings. Can we not pass variable arguments to the right of the function pointer argument? Has it got something to do with the calling sequence (__stdcall, ___pascal etc.)?
No.
Here's the code:
#include <stdio.h>
/*float Add(float, float); float Subtract(float, float); float Multiply(float, float); float Divide(float, float); float (*ptr)(float, float); */
int DoSomething(int, int); int ConsumeFnPointer(int (*ptr1)(int, int));
The ConsumeFnPointer takes one input. The piece 'int (*ptr1)(int,int)' is
one parameter. It is a pointer to a function that takes as input two int
and returns an int.
int main() { //ptr=&Add; //printf("%f",ptr(3,9)); printf("%d", ConsumeFnPointer(&DoSomething,12,4));
Here you are passing ConsumeFnPointer three arguments. This is why it is
complaining. If you want to pass in two integers as well as the function
pointer, you need to declare ConsumeFnPointer as:
int ConsumeFnPointer(int (*)(int, int), int a, int b);
}
/*float Add(float a, float b) { return a+b;} float Subtract(float a, float b){ return a-b;} float Multiply(float a, float b){ return a*b;} float Divide(float a, float b){ return a/b;} */
int ConsumeFnPointer(int (*ptr1)(int a, int b), int a, int b) { return (*ptr1)(a,b); }
int DoSomething(int a, int b) { return 1+a-b; }
--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@whitehouse.gov This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Kylotan |
last post by:
I have the following code:
def IntToRandFloat(x):
"""Given a 32-bit integer, return a float in """
x = int(x)
x = int(x << 13) ^ x
return...
|
by: Gary |
last post by:
Hi! guys,
I have a SQL agent job fails because it gets 10 warnings when it runs
a stored procedure. These warnings are trivial and can be ignored. Can
I make it ignore these warnings and...
|
by: Terry Richards |
last post by:
mysql 4.1.9-standard
how do i find exactly what the warnings are when i only get
Query OK, 1 row affected, 1 warning (0.00 sec)
:-)^2
|
by: prasanna |
last post by:
i will be very thankful if you sent all the errors and warnings regarding
to the language C
thank you
|
by: John Fisher |
last post by:
void f(int p)
{
}
Many (most?) compilers will report that p is unreferenced here. This may not
be a problem as f may have to match some common prototype. Typically
pointers to functions are...
|
by: funkyj |
last post by:
I've been googling around trying to find the answer to this question
but all I've managed to turn up is a 2 year old post of someone else
asking the same question (no answer though).
...
|
by: pete142 |
last post by:
When I compile this code:
typedef unsigned char BYTE;
BYTE *
IpString(unsigned int ip)
{
static BYTE ipString;
ipString = (BYTE) 0xff & (ip >24);
ipString = (BYTE) 0xff & (ip >16);
|
by: gil |
last post by:
Hi,
I'm trying to find the best way to work with compiler warnings. I'd
like to remove *all* warnings from the code, and playing around with
the warning level, I've noticed that compiling with...
|
by: billiejoex |
last post by:
Hi there,
into a module of mine I 'warn' a message if a certain situation
occurs:
def add_anonymous_user(permissions=('r'):
if 'w' in p:
import warnings
warnings.warn("it's not rencommended...
|
by: Robert Singer |
last post by:
Platform: winXP, excel 2003
Python 2.5.2
XLWriter 0.4a3 (http://sourceforge.net/projects/pyxlwriter/)
Is anyone here using this very nice package, for writing excel files?
I'm using it on...
|
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: 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...
|
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: 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,...
|
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,...
|
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
| |