Link to home
Start Free TrialLog in
Avatar of naseeam
naseeamFlag for United States of America

asked on

Shouldn't function pointer always be constant?

The following file defines function pointer prototype.
Filea.h
typedef unsigned char (*pPROGRAM_8BYTE) (unsigned int *dest, unsigned int *src);

A function pointer will be used to call the following function:
File1.c
#include <Filea.h>
/* Function pointer assignment */
const pPROGRAM_8BYTE m32_program_8byte_ptr = (pPROGRAM_8BYTE)  m32_flash_8byte_program;
...
unsigned char m32_flash_8byte_program (unsigned int *dest, unsigned int *src) {
...
}

File1.h
/* macro for normal calling */
#include <Filea.h>
#define m32_program_8byte (*m32_program_8byte_ptr)

extern const pPROGRAM_8BYTE m32_program_8byte_ptr;

somefile.c
#include <File1.h>
#include <Filea.h>
/* Invoke function */
stat = m32_program_8byte(...);


Few Questions:
1.  Will above work? In File1.c, I'm assigning address or function to function pointer before the function definition.  Will that work?
2.  In File1.c, I've make function pointer constant because function location shouldn't change.  Is that correct?  Shouldn't function pointer always be constant?
3.  In File1.c, I have a typecast.  Do I need it?  Why or Why not?


Thanks.



ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oh, and :

>> Why or Why not?

Taking the address of a function already gives it the correct function pointer type.