字串格式宣告
char *const p[4] = {"a01", "b02", "c03", "d04"};
===============================================
char *const p[2]={&x,&y}
取值時使用
*p[0]
*p[1]
===============================================
迴圈取值
char *const p[4] = {"a01", "b02", "c03", "d04"};
for(i = 0; i < 4; i++, j=0)
{
do{
y = *(p + j);
j++;
} while(y != '\0');
}
i=0時,取出a,0,1直到字串尾\0
i=1時,取出b,0,2直到字串尾\0
.
.==========================================
typedef struct{
unsigned char u8alarmTimeSeconds;
unsigned char u8alarmTimeMinutes;
unsigned char u8alarmTimeHours;
}STRUCT_ALARM_Type;
//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;
for(j = 0; j < 5; j++)
{
p_structure[j]->u8alarmTimeSeconds = seconds;
p_structure[j]->u8alarmTimeMinutes = minutes;
p_structure[j]->u8alarmTimeHours = hours;
}
}
.==============================================
函數指標用法
int x,y;
int add(int a,int b);
int sub(int a,int b);
int foobar(int a,int b,int (*fp)(int,int))
{
return fp(a,b);
}
void main(void)
{
x=foobar(5,12,&add);
y=foobar(5,12,&sub);
}
==============================================
函數指標用法
//Variable declarations
unsigned char functionState = 0;
unsigned int functionResult = 0;
//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);
functionState = 0;
//Now use function pointers in program memory
//Calls function1
functionState = 0;
functionResult = funPtrFlash[functionState](2);
//Calls function 2
functionState++;
functionResult = funPtrFlash[functionState](2);
//Calls function 3
functionState++;
functionResult = funPtrFlash[functionState](2);
functionState = 0;
}
}
//Returns the value passed to the function
int function1(int x)
{
return x;
}
//Returns the square of the value passed to the function
int function2(int y)
{
return y * y;
}
//Returns the cube of the value passed to the function
int function3(int z)
{
return z * z * z;
}
=================================================
指標型狀態機架構寫法運用
typedef enum
{
STATE0 = 0,
STATE1,
STATE2
} SYSTEM_STATE;
SYSTEM_STATE gSystemState;
//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)++;
if(InputBits == 0 && *State > 1)
return InputBits;
else
return 99;
}
unsigned int function2(unsigned int InputBits, SYSTEM_STATE *State)
{
(*State)++;
if(InputBits == 2 && *State > 1)
return InputBits * InputBits;
else
return 88;
}
unsigned int function3(unsigned int InputBits, SYSTEM_STATE *State)
{
(*State)++;
if(InputBits == 0 && *State > 1)
return InputBits * InputBits * InputBits;
else
return 77;
}
/* 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;
while(1)
{
gSystemState = STATE0;
result1 = RunStateMachine( 1 );
//gSystemState should be incremented by function
result2 = RunStateMachine( 2 );
//gSystemState should be incremented by function
result3 = RunStateMachine( 3 );
}
}
=====================================================
*P==*(&x)==10
*pp==*(&p)==p(address of x)
**pp==*(*pp)==*(p)==10 |