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?
#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?