ARM Technical Support Knowledge Articles

DIFFERENT BASIC TYPES ERROR FOR FUNCTION POINTER

Applies to: General Topics

Answer


Information in this article applies to:


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.

Article last edited on: 2005-07-19 10:28:33

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential