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
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;
}
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);
5条答案
按热度按时间xurqigkl1#
首先,运算符优先于 * 运算符,()运算符优先于其他所有运算符。
第二,如果你没有把数字运算符赋给任何东西,那么数字运算符和数字运算符是一样的。不同之处在于number返回number,然后递增number,而number先递增,然后返回它。
第三,通过增加一个指针的值,你增加了它的内容的大小,也就是说,你增加它就像你在一个数组中迭代一样。
总结一下
因为这里有很多案例,我可能犯了一些错误,如果我错了,请纠正我。
编辑:
所以我错了,优先级比我写的要复杂一点,在这里查看:http://en.cppreference.com/w/cpp/language/operator_precedence
mdfafbf12#
检查了程序,结果是,
qojgxg4l3#
下面是各种“直接打印”建议的示例。我觉得这很有教育意义。
它回来了
我将指针地址转换为
int
s,以便可以轻松比较它们。我用GCC编译的。
sqougxex4#
关于 “如何增加指针地址和指针的值?“ 我认为
++(*p++);
实际上定义得很好,并且可以满足您的要求,例如:它不是在一个序列点之前修改同一个东西两次。我不认为这是一个很好的风格,虽然对大多数用途-这是一个有点太神秘,我喜欢。
lxkprmvk5#