1. VS2019里面创建C++控制台工程
2. 代码
#include <iostream>
uint8_t buf[3][10] = { {1,2,3},{4,5,6,7,8},{9,0} };
uint8_t len1 = 3,len2 = 5,len3 = 2;
void f1(uint8_t **dstBuf, uint8_t *dstLen)
{
*dstBuf = buf[0];
*dstLen = len1;
}
void f2(uint8_t** dstBuf, uint8_t* dstLen)
{
*dstBuf = buf[1];
*dstLen = len2;
}
void f3(uint8_t** dstBuf, uint8_t* dstLen)
{
*dstBuf = buf[2];
*dstLen = len3;
}
void print_f(uint8_t* buf, uint8_t len)
{
for (int i = 0;i < len;i++)
{
printf("%d ",buf[i]);
}
printf("\n");
}
int main()
{
uint8_t *rxBuf,rxLen;
std::cout << "Hello World!\n";
f1(&rxBuf, &rxLen);
printf("f1:");
print_f(rxBuf, rxLen);
f2(&rxBuf, &rxLen);
printf("f2:");
print_f(rxBuf, rxLen);
f3(&rxBuf, &rxLen);
printf("f3:");
print_f(rxBuf, rxLen);
int i = 3;
int* p;
p = &i;
int** q;
q = &p;
printf("%d", **q);
}
rxBuf的地址&rxBuf = 0x00dff978 作为形参传入f1()
这样dstBuf = &rxBuf = 0x00dff978
*dstBuf = 0x0013c000 就是buf[0]的地址
rxBuf通过调用函数,在函数中修改指针的值,达到指向不同缓存数组的目的。