【问题标题】:Misunderstanding in particular user case of pointers and double-pointers对指针和双指针的特定用户案例的误解
【发布时间】:2021-05-10 13:32:59
【问题描述】:

我正在处理指针、双指针和数组,我想我有点搞砸了。我一直在阅读它,但我的特定用户案例让我感到困惑,如果有人能澄清一下我的想法,我将不胜感激。这是我为表达我的误解而编写的一小段代码:

#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);
}

假设函数fnFindValue_vo 和它的参数不能改变。所以我的目的是找到数组中一个值的相对索引,以第一个非零值作为参考(不需要找到它,可以硬编码)。

在第一种方式中,我是按照我的逻辑和对指针的理解来完成的。所以我有*pu8P2 包含u8Array 的第一个成员的地址,**ppu8P2 包含pu8P2 的地址。所以调用函数后,我只需要减去指向u8Array的指针即可得到相对索引。

无论如何,我尝试了另一种方法。我刚刚创建了一个指针,并将它的地址传递给函数,而不初始化指针。所以后来我只需要减去这两个指针,我也得到了相对索引。

我的困惑来自第二种方法。

  • 为什么ppu8Pos不必初始化,ppu8P2是的? IE。为什么我不能将其声明为uint8_t **ppu8P2;? (它给了我分段错误)。
  • 这两种方法中哪一种更实用/更适合编码?
  • 当函数的参数是双指针时,为什么可以将地址提供给指针?

【问题讨论】:

  • 我建议您使用铅笔和纸绘制数组和指针。将数组绘制为一个长矩形,分成正方形(每个元素一个)。然后画一个正方形,例如puBP2 并从中画一个箭头指向“数组”中的元素 0。然后为ppuB2 绘制一个新正方形,并从它向puBP2 的正方形绘制一个箭头。对您可能拥有的所有其他指针执行相同操作。请记住,当您取消引用指针时(在其上使用一元 * 运算符),您将按照箭头指向它所指向的位置。
  • 问题与指针指向的位置及其内容没有太大关系,而更多的是它们之间的差异,因为我在帖子末尾指出了3个误解。这两种情况我都画了,我知道它们都给出了正确的答案,但我仍然不能通过画图来回答最后 3 点。无论如何,谢谢你!

标签: arrays c pointers double-pointer


【解决方案1】:

老实说,并没有试图完全理解您的代码,但我的建议是不要过度复杂化。

我会从一个帮助我理清思路的事实开始:

如果你有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/

按照 cmets 中的建议将其绘制在纸上会有所帮助,但对我帮助很大的是学习使用调试器和断点。在第一行下一个断点,然后一步一步地运行程序。在“手表”中放置所有变体,例如 pu8P2,&amp;pu8P2,*pu8P2,**pu8P2 看看发生了什么。

【讨论】:

    【解决方案2】:

    指针的地址(例如&amp;pu8P2)是一个指向指针的指针

    &amp;pu8P2 的结果是指向变量pu8P2 的指针。

    由于pu8P2 属于uint8_t * 类型,那么指向这种类型的指针必须是uint8_t **


    关于ppu8Pos,它不需要初始化,因为这发生在fnFindValue_vo函数中,赋值为*vF_ppu8Match = &amp;vF_pu8Msg[i]

    但是这里有一个陷阱:如果条件 vF_u8Value == vF_pu8Msg[i] 永远不会为真,那么分配永远不会发生,ppu8Pos 将保持未初始化状态。所以ppu8Pos的初始化毕竟是真的需要。


    每个解决方案的“实用性”更多是我认为的个人意见问题,所以我没有回答。

    【讨论】:

      【解决方案3】:

      为什么ppu8Pos不必初始化,而ppu8P2是的

      您没有立即使用ppu8Pos 的值。相反,您将其地址传递给另一个函数,在那里它通过引用分配。另一方面,ppu8P2你传递给另一个函数的ppu8Pos的地址,在这个函数中使用了它的值,所以你需要初始化它。

      这两种方法中哪一种更实用/更好的编码实践

      它们在所有意图和目的上都是相同的,出于完全相同的原因,这两个片段是相同的:

      // 1
      double t = sin(x)/cos(x);
      
      // 2
      double s = sin(x), c = cos(x);
      double t = s/c;
      

      在一种情况下,您使用初始化为值的变量。在另一种情况下,您直接使用一个值。值的类型并不重要。它可以是双精度数,也可以是指针,也可以是指向指针的指针。

      当函数的参数是双指针时,为什么可以给指针地址?

      你提到的这两件事,一个指针的地址和一个双指针,是一回事。它们不是两个非常相似的东西,或者几乎无法区分,或者任何类似的弱表述。不,这两个措辞的意思完全一样,小数点后的所有数字。

      【讨论】:

        【解决方案4】:

        对于初学者来说,函数fnFindValue_vo 可能是未定义行为的原因,因为它不会设置指针*vF_ppu8Match,以防在数组中找不到目标值。

        另外很奇怪,数组的大小是由uint8_t 类型的对象指定的。这没有意义。

        函数至少应该以如下方式声明

        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;
        }
        

        您的问题中使用的两种方法之间的区别在于,在第一个代码 sn-p 中,如果找不到目标元素,则指针仍将指向数组的第一个元素

        uint8_t *pu8P2 = &u8Array[0];
        

        还有这个表达

         uint8_t u8Diff1 = *ppu8P2 - pu8Reference;
        

        会产生一些令人困惑的正值(由于 uint8_t 类型),因为差异 *ppu8P2 - pu8Reference 是负数。

        在这种情况下,在第二个代码 sn-p 中,由于此语句,您将获得未定义的行为

        uint8_t u8Diff2 = ppu8Pos - pu8Reference;
        

        因为指针 ppu8Pos 没有被初始化。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-04-10
          • 2016-02-03
          • 2021-07-22
          相关资源
          最近更新 更多