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

我们假设,

int *p;
int a = 100;
p = &a;

下面的代码将实际做什么以及如何做?

p++;
++p;
++*p;
++(*p);
++*(p);
*p++;
(*p)++;
*(p)++;
*++p;
*(++p);

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

注意: 假设 a=5120300的地址存储在地址为 3560200的指针 p中。现在,在执行每个语句之后,p & a的值是什么?

327724 次浏览

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

其次,如果你没有给它们赋值,那么 + + 运算符和数字 + + 运算符是一样的。区别在于 number + + 返回 number,然后递增 number,+ + number 首先递增,然后返回 number。

第三,通过增加一个指针的值,你就是在增加它的内容的大小,也就是说,你是在增加它,就像你在一个数组中迭代一样。

总而言之:

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 pointed at by ptr is incremented
++(*ptr); // The value pointed at by ptr is incremented
++*(ptr); // The value pointed at by 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 pointed at by 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

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

编辑:

所以我错了,先例比我写的要复杂一些,看这里: Http://en.cppreference.com/w/cpp/language/operator_precedence

关于 “如何递增指针地址和指针值?”,我认为 ++(*p++);实际上已经定义得很好了,并且可以做到你所要求的,例如:

#include <stdio.h>


int main() {
int a = 100;
int *p = &a;
printf("%p\n",(void*)p);
++(*p++);
printf("%p\n",(void*)p);
printf("%d\n",a);
return 0;
}

It's not modifying the same thing twice before a sequence point. I don't think it's good style though for most uses - it's a little too cryptic for my liking.

检查了程序,结果如下:

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

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

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

它回来了

(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

我将指针地址转换为 int,这样它们就可以很容易地进行比较。

我和海湾合作委员会一起编译的。

        Note:
1) Both ++ and * have same precedence(priority), so the associativity comes into picture.
2) in this case Associativity is from **Right-Left**


important table to remember in case of pointers and arrays:


operators           precedence        associativity


1)  () , []                1               left-right
2)  *  , identifier        2               right-left
3)  <data type>            3               ----------


let me give an example, this might help;


char **str;
str = (char **)malloc(sizeof(char*)*2); // allocate mem for 2 char*
str[0]=(char *)malloc(sizeof(char)*10); // allocate mem for 10 char
str[1]=(char *)malloc(sizeof(char)*10); // allocate mem for 10 char


strcpy(str[0],"abcd");  // assigning value
strcpy(str[1],"efgh");  // assigning value


while(*str)
{
cout<<*str<<endl;   // printing the string
*str++;             // incrementing the address(pointer)
// check above about the prcedence and associativity
}
free(str[0]);
free(str[1]);
free(str);