[英]How to increment a pointer address and pointer's value?
Let us assume, 我们假设,
int *p;
int a = 100;
p = &a;
What will the following code will do actually and how? 以下代码将实际执行什么以及如何执行?
p++;
++p;
++*p;
++(*p);
++*(p);
*p++;
(*p)++;
*(p)++;
*++p;
*(++p);
I know, this is kind of messy in terms of coding, but I want to know what will actually happen when we code like this. 我知道,这在编码方面有点混乱,但我想知道当我们这样编码时会发生什么。
Note : Lets assume that the address of a=5120300
, it is stored in pointer p
whose address is 3560200
. 注意:假设
a=5120300
的地址,它存储在地址为3560200
指针p
。 Now, what will be the value of p & a
after the execution of each statement? 现在,执行每个声明后,
p & a
的价值是多少?
First, the ++ operator takes precedence over the * operator, and the () operators take precedence over everything else. 首先,++运算符优先于*运算符,而()运算符优先于其他所有运算符。
Second, the ++number operator is the same as the number++ operator if you're not assigning them to anything. 其次,如果您没有将它们分配给任何东西,则++数字运算符与数字++运算符相同。 The difference is number++ returns number and then increments number, and ++number increments first and then returns it.
差异是数字++返回数字然后递增数字,并且++数字首先递增然后返回它。
Third, by increasing the value of a pointer, you're incrementing it by the sizeof its contents, that is you're incrementing it as if you were iterating in an array. 第三,通过增加指针的值,您可以通过其内容的大小来递增它,也就是说,您正在递增它,就好像您在数组中进行迭代一样。
So, to sum it all up: 所以,总结一下:
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
As there are a lot of cases in here, I might have made some mistake, please correct me if I'm wrong. 由于这里有很多案例,我可能犯了一些错误,如果我错了,请纠正我。
EDIT: 编辑:
So I was wrong, the precedence is a little more complicated than what I wrote, view it here: http://en.cppreference.com/w/cpp/language/operator_precedence 所以我错了,优先级比我写的要复杂一点,在这里查看: http : //en.cppreference.com/w/cpp/language/operator_precedence
checked the program and the results are as, 检查程序,结果为,
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
With regards to "How to increment a pointer address and pointer's value?" 关于“如何递增指针地址和指针的值?” I think that
++(*p++);
我认为
++(*p++);
is actually well defined and does what you're asking for, eg: 实际上是很好的定义,并做你要求的,例如:
#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.
虽然对于大多数用途我并不认为它是好的风格 - 但是我觉得它有点太神秘了。
The following is an instantiation of the various "just print it" suggestions. 以下是各种“只是打印它”建议的实例化。 I found it instructive.
我发现它很有启发性。
#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;
}
It returns 它回来了
(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
I cast the pointer addresses to int
s so they could be easily compared. 我将指针地址转换为
int
s,以便可以轻松比较它们。
I compiled it with GCC. 我用GCC编译了它。
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.