我正在尝试创建一个带有嵌套函数的结构,它将结构本身作为参数传递给嵌套函数,以简化调用函数。该代码将使用 xc16 编译器在 Pic 24f 系列 mcu 上运行。另外,我知道 count 函数是多余的,但我认为它说明了我的观点。例如:
结构:
typedef struct
{
uchar Fifo[UART1_RX_MAX_BUFFER_SIZE];
uchar FifoIndex = 0;
uchar FifoCount = 0;
uchar FifoOverrunFlag = FALSE;
uchar (*CountPointer);
uchar (*PutPointer);
}Fifo;
功能:
// Returns: The number of items in the Fifo
uchar FifoGetCount(Fifo *fifo)
{
return fifo->FifoCount;
}
// Summary: Adds data to the end of the Fifo
// Parameters:
// data: Data to be added to the end of the Fifo
// Returns: True (1) if the data was successfully added
// False (0) if the data was not successfully added
// as a result of the Fifo being full
uchar FifoPut(Fifo *fifo, uchar data)
{
if (fifo->FifoCount > FIFO_MAX_SIZE)
{
fifo->FifoOverrunFlag = TRUE;
return FALSE;
}
uint putIndex = fifo->FifoGetPutIndex();
fifo->Fifo[putIndex] = data;
fifo->FifoCount++;
return TRUE;
}
主要的:
Fifo fifo1;
int main()
{
fifo1.CountPointer = FifoGetCount(fifo1);
fifo1.PutPointer = FifoPut(fifo1, uchar data);
// Intended Usage
uchar myCount = fifo1.FifoGetCount();
uchar myData = 1;
fifo1.FifoPut(myData);
}