如何递增指针地址和指针的值?

Din*_*esh 79 c pointers

我们假设,

int *p;
int a = 100;
p = &a;
Run Code Online (Sandbox Code Playgroud)

以下代码将实际执行什么以及如何执行?

p++;
++p;
++*p;
++(*p);
++*(p);
*p++;
(*p)++;
*(p)++;
*++p;
*(++p);
Run Code Online (Sandbox Code Playgroud)

我知道,这在编码方面有点混乱,但我想知道当我们这样编码时会发生什么.

注意:让我们假设它的地址a=5120300,它存储在p地址为的指针中3560200.现在,p & a执行每个语句后的价值是多少?

fel*_*aia 140

首先,++运算符优先于*运算符,而()运算符优先于其他所有运算符.

其次,如果您没有将它们分配给任何东西,则++数字运算符与数字++运算符相同.差异是数字++返回数字然后递增数字,并且++数字首先递增然后返回它.

第三,通过增加指针的值,您可以通过其内容的大小来递增它,也就是说,您正在递增它,就好像您在数组中进行迭代一样.

所以,总结一下:

ptr++;    // Pointer moves to the next int position (as if it was an array)
++ptr;    // Pointer moves to the next int position (as if it was an array)
++*ptr;   // The value of ptr is incremented
++(*ptr); // The value of ptr is incremented
++*(ptr); // The value of ptr is incremented
*ptr++;   // Pointer moves to the next int position (as if it was an array). But returns the old content
(*ptr)++; // The value of ptr is incremented
*(ptr)++; // Pointer moves to the next int position (as if it was an array). But returns the old content
*++ptr;   // Pointer moves to the next int position, and then get's accessed, with your code, segfault
*(++ptr); // Pointer moves to the next int position, and then get's accessed, with your code, segfault
Run Code Online (Sandbox Code Playgroud)

由于这里有很多案例,我可能犯了一些错误,如果我错了,请纠正我.

编辑:

所以我错了,优先级比我写的要复杂一点,在这里查看:http: //en.cppreference.com/w/cpp/language/operator_precedence

  • *ptr ++,值不递增,指针是.这些一元运算符具有相同的优先级,但它们从右到左进行计算.代码意味着"从ptr指向的位置获取内容,然后增加ptr".这是非常常见的C代码(是的,非常令人困惑).请更正此问题,我将删除downvote.对于*(ptr)++,括号不做任何事情. (4认同)
  • @Unheilig第一句话仍然完全错误,postfix ++优先于unary*,它具有与前缀++相同的优先级.除此之外,似乎没问题. (4认同)
  • @felipemaia你确定它会段错吗?也许它只是未定义的行为? (3认同)

Suj*_*mar 11

检查程序,结果为,

p++;    // use it then move to next int position
++p;    // move to next int and then use it
++*p;   // increments the value by 1 then use it 
++(*p); // increments the value by 1 then use it
++*(p); // increments the value by 1 then use it
*p++;   // use the value of p then moves to next position
(*p)++; // use the value of p then increment the value
*(p)++; // use the value of p then moves to next position
*++p;   // moves to the next int location then use that value
*(++p); // moves to next location then use that value
Run Code Online (Sandbox Code Playgroud)

  • @alex使用它意味着例如,考虑语句'int*a = p ++;' 这里指针'p'的第一个值将用于,之后p将移动到下一个位置.因此,在执行上述语句后,实际上'a'将具有由'p'指向的先前位置的地址,并且'p'将指向下一个位置.首先使用'p'的值作为上面的赋值表达式,然后将'p'的值增加到指向下一个位置 (2认同)

Ric*_*one 6

以下是各种“直接打印”建议的实例。我发现它很有启发性。

#include "stdio.h"

int main() {
    static int x = 5;
    static int *p = &x;
    printf("(int) p   => %d\n",(int) p);
    printf("(int) p++ => %d\n",(int) p++);
    x = 5; p = &x;
    printf("(int) ++p => %d\n",(int) ++p);
    x = 5; p = &x;
    printf("++*p      => %d\n",++*p);
    x = 5; p = &x;
    printf("++(*p)    => %d\n",++(*p));
    x = 5; p = &x;
    printf("++*(p)    => %d\n",++*(p));
    x = 5; p = &x;
    printf("*p++      => %d\n",*p++);
    x = 5; p = &x;
    printf("(*p)++    => %d\n",(*p)++);
    x = 5; p = &x;
    printf("*(p)++    => %d\n",*(p)++);
    x = 5; p = &x;
    printf("*++p      => %d\n",*++p);
    x = 5; p = &x;
    printf("*(++p)    => %d\n",*(++p));
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它返回

(int) p   => 256688152
(int) p++ => 256688152
(int) ++p => 256688156
++*p      => 6
++(*p)    => 6
++*(p)    => 6
*p++      => 5
(*p)++    => 5
*(p)++    => 5
*++p      => 0
*(++p)    => 0
Run Code Online (Sandbox Code Playgroud)

我将指针地址强制转换为ints,以便可以轻松比较它们。

我用 GCC 编译它。