Keil Logo

GENERAL: DIFFERENT BASIC TYPES ERROR FOR FUNCTION POINTER


Information in this article applies to:

  • C166 Version 3.12
  • C166 Version 4.03
  • C251 Version 2.14
  • C51 Version 5.50

QUESTION

I wish to assign a function to a function pointer, but my code generates an error stating:

Redefinition of identifier; different basic types

My code is below. What am I doing wrong?

void foo(void);
void (*func_ptr)(void);
func_ptr = foo;
void foo( void ) { ; }
void main(void)
{
  func_ptr();
}

ANSWER

You are trying to assign the function pointer outside of a function. This is not allowed unless you are also declaring the function pointer at the same time. The result is that the compiler is interpreting that line as a declaration of an int called func_ptr (as there is no type specifer, it defaults to int).

This is a redefinition, plus it is of a different type (int) compared to the original definition (function pointer), hence the error.

The solution is to assign the function pointer when declaring the pointer, or inside of a function.

MORE INFORMATION

Refer to a good book on C, such as The C Programming Language by Kernighan & Ritchie.

Last Reviewed: Tuesday, July 19, 2005


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.