Function pointer arguments

J

Jonathan Green

I have some questions about the behavior of the code below.

#include <stdio.h>

void test(void *v);

int main(void)
{
int i=1;
int j=2;
void (*fp)();

fp = test;
(*fp)(&i, j);
test(&i);

return 0;
}

void test(void *v)
{
printf("address is %p\n", v);

return;
}

I compiled the code using gcc -Wall -ansi -pedantic. No warnings or
errors were issued.

1. Why was no error issued for the extra argument when using the
function pointer? If I put an extra argument when calling "test" in
the following line, an error is given. Thats what I would expect.

2. What's the behavior when running the code with the extra argument?

3. The compiler kind of worried me when it didn't give any warnings.
Now I'm not sure how function pointers are handled. Do I need a cast
when I correct the bad line and use (*fp)(&i) in the code above?
 
T

Thomas Lumley

I have some questions about the behavior of the code below.

#include <stdio.h>

void test(void *v);

int main(void)
{
int i=1;
int j=2;
void (*fp)();

fp = test;
(*fp)(&i, j);
test(&i);

return 0;

}

void test(void *v)
{
printf("address is %p\n", v);

return;

}

I compiled the code using gcc -Wall -ansi -pedantic. No warnings or
errors were issued.

1. Why was no error issued for the extra argument when using the
function pointer? If I put an extra argument when calling "test" in
the following line, an error is given. Thats what I would expect.

fp is defined as a function taking unspecified arguments and not
returning a value. This is compatible with how it is used, so there
is no constraint violation.

Tracking the actual type of the function pointer would be fairly easy
here but is hard in general and is not required.

2. What's the behavior when running the code with the extra argument?
Undefined.

3. The compiler kind of worried me when it didn't give any warnings.
Good.

Now I'm not sure how function pointers are handled. Do I need a cast
when I correct the bad line and use (*fp)(&i) in the code above?

No, but you do want to specify the function pointer more completely
void (*fp)(void *);
would specify a single argument of type void *, and then you would get
a diagnostic from calling it with two arguments. On one compiler I
get
test2.c: In function 'main':
test2.c:12: error: too many arguments to function 'fp'

-thomas
 
B

borophyll

I have some questions about the behavior of the code below.

#include <stdio.h>

void test(void *v);

int main(void)
{
int i=1;
int j=2;
void (*fp)();

fp = test;
(*fp)(&i, j);
test(&i);

return 0;

}

void test(void *v)
{
printf("address is %p\n", v);

return;

}

I compiled the code using gcc -Wall -ansi -pedantic. No warnings or
errors were issued.

1. Why was no error issued for the extra argument when using the
function pointer? If I put an extra argument when calling "test" in
the following line, an error is given. Thats what I would expect.

It's because you have declared the type of fp to "pointer to a
function that returns nothing and that accepts an unknown number of
parameters". Try changing the declaration to void (*fp)(void *);
2. What's the behavior when running the code with the extra argument?
Undefined

3. The compiler kind of worried me when it didn't give any warnings.
Now I'm not sure how function pointers are handled. Do I need a cast
when I correct the bad line and use (*fp)(&i) in the code above?

When you declare a function or pointer to function with an empty
parameter list, you are essentially telling the compiler not to check
the number and types of the parameters. This is bad practice, and is
only allowed for historical reasons. In your case your declaration is
simply incorrect. If you really want to declare a function or pointer
to function which accept no parameters, then put the keyword void
inside the parameter list.
 
J

Jonathan Green

No, but you do want to specify the function pointer more completely
void (*fp)(void *);
would specify a single argument of type void *, and then you would get
a diagnostic from calling it with two arguments. On one compiler I
get
test2.c: In function 'main':
test2.c:12: error: too many arguments to function 'fp'

I thought I was missing something. That explains it. Thanks.
 
F

Flash Gordon

Thomas Lumley wrote, On 02/10/07 03:13:
No, but you do want to specify the function pointer more completely
void (*fp)(void *);
would specify a single argument of type void *, and then you would get
a diagnostic from calling it with two arguments.

<snip>

Without that change you *do* require a cast because int* and void* are
not the same thing. Without the cast it will work on most
implementations, but it is not required to. Once you have provided a
prototype, however, no cast is required because the compiler knows it
has to do the conversion.
 

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,815
Messages
2,569,702
Members
45,493
Latest member
RosalieBag

Latest Threads

Top