[英]Misunderstanding in particular user case of pointers and double-pointers
我正在处理指针、双指针和 arrays,我想我有点搞砸了。 我一直在阅读它,但是我的特定用户案例让我感到困惑,如果有人能理清我的想法,我将不胜感激。 这是我为表明我的误解而构建的一小段代码:
#include <stdio.h>
#include <stdint.h>
void fnFindValue_vo(uint8_t *vF_pu8Msg, uint8_t vF_u8Length, uint8_t **vF_ppu8Match, uint8_t vF_u8Value)
{
for(int i=0; i<vF_u8Length; i++)
{
if(vF_u8Value == vF_pu8Msg[i])
{
*vF_ppu8Match = &vF_pu8Msg[i];
break;
}
}
}
int main()
{
uint8_t u8Array[]={0,0,0,1,2,4,8,16,32,64};
uint8_t *pu8Reference = &u8Array[3];
/*
* Purpose: Find the index of a value in u8Array from a reference
* Reference: First non-zero value
* Condition: using the function with those input arguments
*/
// WAY 1
uint8_t *pu8P2 = &u8Array[0];
uint8_t **ppu8P2 = &pu8P2;
fnFindValue_vo(u8Array,10,ppu8P2,16); // Should be diff=4
uint8_t u8Diff1 = *ppu8P2 - pu8Reference;
printf("Diff1: %u\n", u8Diff1);
// WAY 2
uint8_t* ppu8Pos; // Why this does not need to be initialized and ppu8P2 yes
fnFindValue_vo(u8Array,10,&ppu8Pos,64); // Should be diff=6
uint8_t u8Diff2 = ppu8Pos - pu8Reference;
printf("Diff2: %u\n", u8Diff2);
}
假设 function fnFindValue_vo
及其 arguments 无法更改。 所以我的目的是在数组中找到一个值的相对索引,以第一个非零值作为参考(不需要找到它,可以硬编码)。
在第一种方式中,我按照我的逻辑和对指针的理解完成了它。 所以我有*pu8P2
包含 u8Array 的第一个成员的地址,而**ppu8P2
u8Array
pu8P2
的地址。 所以在调用函数之后,我只需要减去指向u8Array
的指针就可以得到相对索引。
无论如何,我尝试了另一种方法。 我刚刚创建了一个指针,并将它的地址传递给函数,而不初始化指针。 所以后来我只需要减去这两个指针,我也得到了相对索引。
我的困惑来自第二种方法。
ppu8Pos
不必初始化,而ppu8P2
是的? 即为什么我不能将它声明为uint8_t **ppu8P2;
? (它给了我分段错误)。指针的地址(例如&pu8P2
)是指向指针的指针。
&pu8P2
的结果是指向变量pu8P2
的指针。
由于pu8P2
是uint8_t *
类型,那么指向这种类型的指针必须是uint8_t **
。
关于ppu8Pos
,它不需要初始化,因为这发生在fnFindValue_vo
function 中,赋值为*vF_ppu8Match = &vF_pu8Msg[i]
。
但是这里有一个陷阱:如果条件vF_u8Value == vF_pu8Msg[i]
永远不会为真,那么分配永远不会发生并且ppu8Pos
将保持未初始化状态。 所以毕竟确实需要ppu8Pos
的初始化。
我相信每个解决方案的“实用性”更多是个人意见的问题,所以我没有回答。
为什么 ppu8Pos 不必初始化,而 ppu8P2 是
您没有立即使用ppu8Pos
的值。 相反,您将其地址传递给另一个 function,在那里它通过引用分配。 另一方面, ppu8P2
是你传递给另一个function的ppu8Pos
的地址,这里用到了它的值,所以你需要初始化它。
这两种方法中哪一种更实用/更好的编码实践
它们在所有意图和目的上都是相同的,出于完全相同的原因,这两个片段是相同的:
// 1
double t = sin(x)/cos(x);
// 2
double s = sin(x), c = cos(x);
double t = s/c;
在一种情况下,您使用初始化为值的变量。 在另一种情况下,您直接使用一个值。 值的类型并不重要。 它可以是双精度数、指针或指向指针的指针。
当函数的参数是双指针时,为什么可以将地址提供给指针?
你提到的这两件事,一个指针的地址和一个双指针,是一回事。 它们不是两个非常相似的东西,或者几乎无法区分,或者任何类似的弱表述。 不,对于小数点后的所有数字,这两个措辞的含义完全相同。
对于初学者来说,function fnFindValue_vo
可能是未定义行为的原因,因为它不会设置指针*vF_ppu8Match
以防在数组中找不到目标值。
同样奇怪的是,数组的大小由uint8_t
类型的 object 指定。 这没有任何意义。
function 应至少按以下方式声明
void fnFindValue_vo( const uint8_t *vF_pu8Msg, size_t vF_u8Length, uint8_t **vF_ppu8Match, uint8_t vF_u8Value )
{
const uint8_t *p = vF_pu8Msg;
while ( p != vF_pu8Msg + vF_u8Length && *p != vF_u8Value ) ++p;
*vF_ppu8Match = ( uint8_t * )p;
}
您的问题中使用的两种方法之间的区别在于,在第一个代码片段中,如果找不到目标元素,则指针仍将指向数组的第一个元素
uint8_t *pu8P2 = &u8Array[0];
而这个表达
uint8_t u8Diff1 = *ppu8P2 - pu8Reference;
将产生一些令人困惑的正值(由于类型uint8_t
),因为差异*ppu8P2 - pu8Reference
为负。
在这种情况下,在第二个代码片段中,由于此语句,您将获得未定义的行为
uint8_t u8Diff2 = ppu8Pos - pu8Reference;
因为指针 ppu8Pos 没有被初始化。
老实说,不要试图完全理解你的代码,但我的建议是不要让它过于复杂。
我将从一个帮助我理清思路的事实开始:
如果你有int a[10]
; 那么 a 是一个指针,实际上int x = a[2]
和int x = *(a+2)
完全一样——你可以试试。
所以让我们有
int a[10]; //this is an array
//a is a pointer to the begging of the array
a[2] is an int type and it is the third value in that array stored at memory location a plus size of two ints;
&a[2] is a pointer to that third value
*(a) is the first value in the array a
*(a+1) is the same as a[1] and it is the second int value in array a
and finally
**a is the same as *(*a) which means: *a is take the first int value in the array a (the same as above) and the second asterisk means "and take that int and pretend it is a pointer and take the value from the that location" - which is most likely a garbage.
https://stackoverflow.com/questions/42118190/dereferencing-a-double-pointer
Only when you have a[5][5]; then a[0] would be still a pointer to the first row and a[1] would be a pointer to the second row and **(a) would then be the same as a[0][0].
https://beginnersbook.com/2014/01/2d-arrays-in-c-example/
按照评论中的建议在纸上绘制它会有所帮助,但对我帮助很大的是学习使用调试器和断点。 在第一行放一个断点,然后 go 逐步通过程序。 在“手表”中放入所有变体,如pu8P2
、 &pu8P2
、 *pu8P2
、 **pu8P2
,看看发生了什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.