简体   繁体   English

我是指针概念的新手,因为我正在学习如何使用指针指针的递增

[英]I'm new to pointer to pointer concept as I'm learning how to use the incrementing in pointer to pointer

Why I'm getting some garbage value when I increment like this **pptr++ but not for *ptr++?为什么当我像这样增加 **pptr++ 而不是 *ptr++ 时会得到一些垃圾值? Can anyone help me?谁能帮我?

#include<stdio.h>
int main()
{
    static int array[] ={9,1,2,3,4}; 

    int *ptr = array;
    int **pptr = &ptr;


    **pptr++;
    printf("%d",**pptr );

    *ptr++;
    printf("%d",*ptr );

    return 0;
}

**pptr++; and *ptr++;*ptr++; are incrementing the pointer, not what is pointed at by them.正在递增指针,而不是它们指向的内容。

pptr is pointing at ptr , which can be seen as one-element array. pptr指向ptr ,可以看作是一个元素数组。 Incrementing pptr will move the pointer to out-of-range and disallow dereferencing that.递增pptr会将指针移到超出范围并禁止取消引用。

ptr is pointing at the first element of the 5-element array array . ptr指向 5 元素数组array的第一个元素。 Incrementing ptr will move the pointer to point at the 2nd element.递增ptr会将指针移动到指向第二个元素的位置。 You are still allowed to dereference this.您仍然可以取消引用它。

The pointer pptr points to the pointer (object) ptr due to these declaration.由于这些声明,指针pptr指向指针(对象) ptr

int *ptr = array;
int **pptr = &ptr;

This expression这个表达

**pptr++;

may be rewritten just like可以像这样重写

pptr++;

because applying the dereferencing operator does not have an effect.因为应用解引用运算符没有效果。

So now the pointer after incrementing pptr points to the memory beyond the pointer ptr and dereferencing it in the following statement所以现在增加pptr后的指针指向指针ptr之外的 memory 并在以下语句中取消引用它

printf("%d",**pptr );

results in undefined behavior.导致未定义的行为。

Instead of these two statements而不是这两个语句

**pptr++;
printf("%d",**pptr );

you could write just one statement你可以只写一个语句

printf("%d",**pptr++ );

and the output will be output 将是

9

though post-incrementing the pointer does not make a sense.尽管后递增指针没有意义。

As for these statements至于这些说法

*ptr++;
printf("%d",*ptr );

then again it may be rewritten like然后它可能会被重写为

ptr++;
printf("%d",*ptr );

because dereferencing the pointer in this expression *ptr++ does not have an effect.因为在这个表达式*ptr++中取消引用指针没有效果。

As the pointer ptr points to the first element of the array array then after incrementing it it points to the second element of the array.由于指针ptr指向数组array的第一个元素,然后在递增它之后指向数组的第二个元素。

Thus this call因此这个电话

printf("%d",*ptr );

outputs输出

1

To make it more clear you may consider this declaration为了更清楚,你可以考虑这个声明

int **pptr = &ptr;

as a declaration of a pointer that points to the first element of an array with only single element.作为指向只有单个元素的数组的第一个元素的指针的声明。 Thus incrementing the pointer results that the pointer will point beyond the array with a single element.因此,递增指针会导致指针指向具有单个元素的数组之外。

Unlike the pointer pptr the pointer ptr points to the first element of an array that contains more than one element.与指针pptr不同,指针ptr指向包含多个元素的数组的第一个元素。 So after incrementing the pointer it will point to a valid object: the second element of the array.所以在增加指针后,它将指向一个有效的 object:数组的第二个元素。

That is the difference.这就是区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM