1

Consider this program:

int func2(char x, char y)
{
    return x+y;
}

int (*fp1)(char);

void main() 
{ 
    fp1 = func2; /* func2 has one more argument than fp1 */
} 

Is the final assignment C90-compliant?

Keil C51 v9.06 accepts the program without warnings, while gcc complains with

warning: assignment from incompatible pointer type

I would like to know if this a bug in the Keil compiler or in fact a C90 compliant way to deal with this.

UPDATE: According to this answer on C99, the corresponding cast is legal. But if you invoke the function pointer you get undefined behavior. Does this mean that the assignment is also legal?

Community
  • 1
  • 1
Jakob Buron
  • 1,196
  • 11
  • 19
  • Are your warnings pedantic for your Keil C51 v9.06 ? If not, make them so and check again. – WhozCraig Mar 04 '13 at 11:45
  • I'm using the highest warning level (2 for this compiler). – Jakob Buron Mar 04 '13 at 11:46
  • That is most-unfortunate, then, and should be filed as a defect to the manufacturer. [Interesting read](http://www.keil.com/support/man/docs/c51/c51_intro.htm) on that compiler in general, btw. 3-byte pointers? Wow. that must be fun. – WhozCraig Mar 04 '13 at 12:05
  • per my knowledge of C, it is standard rule that to define function pointer, all the function must have same no of arguments, argument types and function return type must be same. – Kinjal Patel Mar 04 '13 at 12:08

2 Answers2

1

The real question is WHY are you trying to do this?

As soon as you call fp1 when it pointing to func2, func2 will return garbage because argument 2 is not passed (it'll be some random value on the stack, or in a CPU register).

Anonymouse
  • 935
  • 9
  • 20
  • The question is whether the final assignment is C90-compliant or not. I am well aware that calling fp1 will fail. – Jakob Buron Mar 06 '13 at 09:57
0

when you build call for function func2 using function pointer fp1, there will be only one value passed on the stack, But in actual, function2 will refer 2nd value on stack as well. So either program will give segmentation fault and fail. Or if does not fail 2nd argument will have garbage value.

rahul.deshmukhpatil
  • 977
  • 1
  • 16
  • 31