让我们假设
int *p;
int a = 100;
p = &a;
以下代码将实际执行什么操作以及如何执行?
p++;
++p;
++*p;
++(*p);
++*(p);
*p++;
(*p)++;
*(p)++;
*++p;
*(++p);
我知道,这在编码方面有点麻烦,但是我想知道当我们这样编码时实际会发生什么。
注意:假设的地址a=5120300
存储在p
地址为的指针中3560200
。现在,p & a
每条语句执行后的值是多少?
让我们假设
int *p;
int a = 100;
p = &a;
以下代码将实际执行什么操作以及如何执行?
p++;
++p;
++*p;
++(*p);
++*(p);
*p++;
(*p)++;
*(p)++;
*++p;
*(++p);
我知道,这在编码方面有点麻烦,但是我想知道当我们这样编码时实际会发生什么。
注意:假设的地址a=5120300
存储在p
地址为的指针中3560200
。现在,p & a
每条语句执行后的值是多少?
printf
将使用%p打印一个指针
Answers:
首先,++运算符优先于*运算符,而()运算符优先于其他所有运算符。
其次,如果您没有将++ number运算符分配给任何东西,则它与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 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
由于这里有很多情况,我可能犯了一些错误,如果我写错了,请纠正我。
编辑:
所以我错了,优先级比我写的要复杂一些,请在此处查看:http : //en.cppreference.com/w/cpp/language/operator_precedence
检查了程序,结果是,
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
s,以便可以轻松进行比较。
我用GCC编译了它。
关于“如何增加指针地址和指针的值?” 我认为这++(*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;
}
它不是在序列点之前两次修改同一件事。我认为对于大多数用途来说,这不是一个好风格-对我来说有点神秘。
++*p++
将成功地增加value和指针(后缀++
绑定比dereference绑定更强*
,并且++
由于顺序而发生在前缀之前)。仅当需要在增加值之前使用括号时,才需要使用括号(*p++)++
。如果使用全前缀,++*++p
也可以在没有括号的情况下正常工作(但在指针增加之后增加指向的值)。
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);