//Create array of pointers to point to 5 instances of STRUCT_ALARM_Type. Also create 5 instances of the structure.
STRUCT_ALARM_Type STRUCT_alarmDescriptor[5];
STRUCT_ALARM_Type *p_STRUCT_alarmDescriptor[5] = {&STRUCT_alarmDescriptor[0], &STRUCT_alarmDescriptor[1],
&STRUCT_alarmDescriptor[2], &STRUCT_alarmDescriptor[3], &STRUCT_alarmDescriptor[4]};
//Create a single pointer to show how a single pointer can also be used for access
STRUCT_ALARM_Type *p_STRUCT_singlePointer = &STRUCT_alarmDescriptor[0];
//Function prototype for passing single pointer
void load_alarm_data_using_pointer(STRUCT_ALARM_Type *p_structure, unsigned char seconds,
unsigned char minutes, unsigned char hours);
//Function prototype for passing array of pointers
void load_alarm_data_using_pointer_array(STRUCT_ALARM_Type *p_structure[ ], unsigned char seconds,
unsigned char minutes, unsigned char hours);
int main(int argc, char** argv)
{
unsigned char i;
for(i = 0; i < 5; i++)
{
//Load values directly with the pointer
p_STRUCT_alarmDescriptor->u8alarmTimeMinutes = 1;
p_STRUCT_alarmDescriptor->u8alarmTimeSeconds = 2;
p_STRUCT_alarmDescriptor->u8alarmTimeHours = 3;
//Pass the address to change the actual alarmDescriptor location
load_alarm_data_using_pointer(&STRUCT_alarmDescriptor, 4, 5, 6);
//Pass the address using the pointer
load_alarm_data_using_pointer(p_STRUCT_alarmDescriptor, 7, 8, 9);
//Set the structure values using a single pointer
load_alarm_data_using_pointer(p_STRUCT_singlePointer, 0xA, 0xB, 0xC);
//Increment the single pointer
p_STRUCT_singlePointer++;
}
//Pass array of pointers to the function
load_alarm_data_using_pointer_array(&p_STRUCT_alarmDescriptor[0], 77, 88, 99);
while(1);
}
//Receive one pointer and act on that location
void load_alarm_data_using_pointer(STRUCT_ALARM_Type *p_structure, unsigned char seconds,
unsigned char minutes, unsigned char hours)
{
p_structure->u8alarmTimeSeconds = seconds;
p_structure->u8alarmTimeMinutes = minutes;
p_structure->u8alarmTimeHours = hours;
}
//Receive the array of pointers address and load all five instances of the array
void load_alarm_data_using_pointer_array(STRUCT_ALARM_Type *p_structure[ ], unsigned char seconds,
unsigned char minutes, unsigned char hours)
{
unsigned char j;
//Function prototypes
int function1(int x);
int function2(int y);
int function3(int z);
//Function Pointer Declarations and Initialization
// An array of function pointers is declared and the array will contain three
// addresses for function1, function2, and function3. The array is initialized
// with the addresses of these functions. This array will point to functions that
// are passed one int parameter and return one int parameter.
int (*funPtr[3])(int a) = { &function1, &function2, &function3 };
//Place function pointers in program memory
int (* const funPtrFlash[3])(int a) = { &function1, &function2, &function3 };
//因為 const前面有* 所以此宣告為放入FLASH非RAM
//*******************************************************************************
//Could place the function pointers in flash if desired. Uncomment this declaration and comment
// out the existing declaration for funPtr[ ]
//int (*funPtr[3])(int a) __attribute__((space(prog))) = { &function1, &function2, &function3};
//*******************************************************************************
//Main routine
int main(void)
{
//The functionState variable is incremented after each indirect function call so that
// the succeeding function is called next. In each function call, a value of 2 is passed
// to the function
while(1)
{
//Calls function1
functionState = 0;
functionResult = funPtr[functionState](2);
//Calls function 2
functionState++;
functionResult = funPtr[functionState](2);
//Calls function 3
functionState++;
functionResult = funPtr[functionState](2);
//These functions are called indirectly through a function pointer. They are passed a
// value by the RunStateMachine function and return either the InputBits value that is
// passed to it or a literal value. Each function also increments the state machine.
unsigned int function1(unsigned int InputBits, SYSTEM_STATE *State)
{
(*State)++;
/* Array of function pointers to functions of form
* 'unsigned (int fName[ ])(unsigned int, *SYSTEM_STATE)'.
* All functions implementing state logic must have a definition of this form. */
unsigned int ( * pStateFunction[] ) ( unsigned int InputBits, SYSTEM_STATE *State ) =
{
function1,
function2,
function3
};
//上面宣告 針對 function1, function2, function3 ;通常前面會加上& ,
//即 &function1, &function2, &function3
//使用上有無&並無影響函數指標的宣告,但建議增加會比較"明顯"。
//Function that is called to run the state machine. It is called by the main( ) program
// and it calls the proper function based on the State variable.
unsigned int RunStateMachine( unsigned int InputBits )
{
return (*pStateFunction[ gSystemState ])( InputBits, &gSystemState );
}
void main(void)
{
volatile unsigned int result1;
volatile unsigned int result2;
volatile unsigned int result3;