用法:用一个变量管理多个指针型的变量,即动态给某个(些)指针型变量赋值
1.给一个指针变量赋值,如(见后面的例子程序):
osalTaskRec_t **ptr;
ptr = &tasksHead;
*ptr = newTask;
其中:
osalTaskRec_t *tasksHead;
osalTaskRec_t *newTask;
ptr可以通过&获得一个指针变量的地址(tasksHead是一个指针变量),以后可以通过*ptr给这个指针变量赋值,赋的值应该也是指针类型类型的变量或值
又如:
ptr = &srchTask->next;
srchTask = srchTask->next;
ptr首先获取srchTask结构体中的next变量的地址,而srchTask则赋值为srchTask结构体中next所指结构体地址。因此可以通过*ptr指针对上一个结构体中的next指针进行赋值(达到通过一个变量ptr管理多个指针变量的目的),而srchTask则可以操纵下一个结构体内部的变量。
eg:该代码取自:ZStack1.4.2中的OSAL_Tasks.c中的osalTaskAdd函数
/***************************************************************************
* @fn osalTaskAdd
*
* @brief Add a task to the task list. Keep task queue in priority order.
*
* @param none
*
* @return
*/
void osalTaskAdd( pTaskInitFn pfnInit,
pTaskEventHandlerFn pfnEventProcessor,
byte taskPriority)
{
osalTaskRec_t *newTask;
osalTaskRec_t *srchTask;
osalTaskRec_t **ptr;
newTask = osal_mem_alloc( sizeof( osalTaskRec_t ) );
if ( newTask )
{
// Fill in new task
newTask->pfnInit = pfnInit;
newTask->pfnEventProcessor = pfnEventProcessor;
newTask->taskID = taskIDs++;
newTask->taskPriority = taskPriority;
newTask->events = 0;
newTask->next = (osalTaskRec_t *)NULL;
// 'ptr' is the address of the pointer to the new task when the new task is
// inserted. Initially it is set to address of 'tasksHead' in case the new
// task is higher priority than the existing head or the queue is empty.
ptr = &tasksHead;
srchTask = tasksHead;
while (srchTask) {
if (newTask->taskPriority > srchTask->taskPriority) {
// insert here. New task has a higher priority than the task
// with which is being compared and a lower or equal priority
// to any task that precedes it.
newTask->next = srchTask;
*ptr = newTask;
return;
}
// set 'ptr' to address of the pointer to 'next' in the current
// (soon to be previous) task control block
ptr = &srchTask->next;
srchTask = srchTask->next;
}
// We're at the end of the current queue. New task is not higher
// priority than any other already in the list. Make it the tail.
// (It is also the head if the queue was initially empty.)
*ptr = newTask;
}
return;
}
阅读(1337) | 评论(0) | 转发(0) |