Why these warnings?

S

Sathyaish

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;
}
 
S

Stephen Sprunk

Sathyaish said:
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
 
V

Vijay Kumar R Zanvar

Sathyaish said:
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 */
}
 
M

Martin Dickopp

Vijay Kumar R Zanvar said:
[...] 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
 
V

Vijay Kumar R Zanvar

Martin Dickopp said:
Vijay Kumar R Zanvar said:
[...] 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.
 
D

Darrell Grainger

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);
 

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

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top