C++ 指针表达式:*ptr++*++;ptr和++*ptr
最近我遇到了这个我自己都无法理解的问题 这三个表达到底是什么意思C++ 指针表达式:*ptr++*++;ptr和++*ptr,c++,c,pointers,increment,C++,C,Pointers,Increment,最近我遇到了这个我自己都无法理解的问题 这三个表达到底是什么意思 *ptr++ *++ptr ++*ptr 我试过里奇。但不幸的是,他无法理解他所说的这3次行动 我知道执行这些操作都是为了增加指针/指向的值。我还可以猜测,可能有很多关于优先级和评估顺序的事情。就像一个人先增加指针,然后获取该指针的内容一样,一个人只需获取内容,然后增加指针等等。正如你所看到的,我对他们的实际操作没有清晰的理解,我希望尽快清除这些操作。但当我有机会将它们应用到程序中时,我真的迷失了方向。例如: int main(
*ptr++
*++ptr
++*ptr
我试过里奇。但不幸的是,他无法理解他所说的这3次行动
我知道执行这些操作都是为了增加指针/指向的值。我还可以猜测,可能有很多关于优先级和评估顺序的事情。就像一个人先增加指针,然后获取该指针的内容一样,一个人只需获取内容,然后增加指针等等。正如你所看到的,我对他们的实际操作没有清晰的理解,我希望尽快清除这些操作。但当我有机会将它们应用到程序中时,我真的迷失了方向。例如:
int main()
{
char *p = "Hello";
while(*p++)
printf("%c",*p);
return 0;
}
给我这个输出:
ello
但我的期望是它会打印Hello
。
最后一个请求——请给我一些示例,说明每个表达式在给定的代码段中是如何工作的。因为大多数时候,只有一段理论从我的头上飞过。
*ptr++:post increment a pointer ptr
*++ptr:预先增加指针ptr
++*ptr:在ptr位置预先增加值
了解增量前和增量后运算符
这将提供
Hello
作为输出
int main()
{
const char *p = "Hello";
while(*p)
printf("%c",*p++);//Increment the pointer here
return 0;
}
*ptr++:后增量指针ptr
*++ptr:预先增加指针ptr
++*ptr:在ptr位置预先增加值
了解增量前和增量后运算符
这将提供
Hello
作为输出
int main()
{
const char *p = "Hello";
while(*p)
printf("%c",*p++);//Increment the pointer here
return 0;
}
假设
ptr
指向数组arr
的第i个元素
*ptr++
计算为arr[i]
,并将ptr
设置为指向arr
的第(i+1)个元素。它相当于*(ptr++)
*++ptr
设置ptr
指向arr
的第(i+1)个元素,并计算为arr[i+1]
。它相当于*(++ptr)
++*ptr
将arr[i]
增加1,并计算为其增加值;指针ptr
保持不变。它相当于++(*ptr)
(*ptr)+
将arr[i]
增加1,并在增加前计算其值;指针ptr
再次保持不变剩下的你可以自己搞清楚;@Jaguar也回答了这个问题。假设
ptr
指向数组的第i个元素arr
*ptr++
计算为arr[i]
,并将ptr
设置为指向arr
的第(i+1)个元素。它相当于*(ptr++)
*++ptr
设置ptr
指向arr
的第(i+1)个元素,并计算为arr[i+1]
。它相当于*(++ptr)
++*ptr
将arr[i]
增加1,并计算为其增加值;指针ptr
保持不变。它相当于++(*ptr)
(*ptr)+
将arr[i]
增加1,并在增加前计算其值;指针ptr
再次保持不变剩下的你可以自己搞清楚;@Jaguar也回答了这个问题。您的优先级是正确的,请注意,
*
优先于前缀增量,但不优先于后缀增量。以下是这些细分的方式:
*ptr++
-从左到右,取消对指针的引用,然后增加指针值(由于后缀优先于取消引用,因此指针值不是它所指向的值)
*++ptr
-递增指针,然后取消引用,这是因为前缀和取消引用具有相同的优先级,因此按从右到左的顺序计算
++*ptr
-在优先级方面与上述类似,再次从右到左取消对指针的引用,然后增加指针指向的内容。请注意,在您的情况下,这将导致未定义的行为,因为您试图修改只读变量(char*p=“Hello”
)。关于优先级,请注意*
优先于前缀增量,但不优先于后缀增量。以下是这些细分的方式:
*ptr++
-从左到右,取消对指针的引用,然后增加指针值(由于后缀优先于取消引用,因此指针值不是它所指向的值)
*++ptr
-递增指针,然后取消引用,这是因为前缀和取消引用具有相同的优先级,因此按从右到左的顺序计算
++*ptr
-在优先级方面与上述类似,再次从右到左取消对指针的引用,然后增加指针指向的内容。请注意,在您的情况下,这将导致未定义的行为,因为您试图修改只读变量(char*p=“Hello”
)。循环中的条件不好:
while(*p++)
printf("%c",*p);
与
while(*p)
{
p++;
printf("%c",*p);
}
这是错误的,这应该是:
while(*p)
{
printf("%c",*p);
p++;
}
*ptr++
与*(ptr++)
相同,即:
const char *ptr = "example";
char value;
value = *ptr;
++ptr;
printf("%c", value); // will print 'e'
const char *ptr = "example";
char value;
++ptr;
value = *ptr;
printf("%c", value); // will print 'x'
const char *ptr = "example";
char value;
value = *ptr;
++value;
printf("%c", value); // will print 'f' ('e' + 1)
*++ptr
与*(++ptr)
相同,即:
const char *ptr = "example";
char value;
value = *ptr;
++ptr;
printf("%c", value); // will print 'e'
const char *ptr = "example";
char value;
++ptr;
value = *ptr;
printf("%c", value); // will print 'x'
const char *ptr = "example";
char value;
value = *ptr;
++value;
printf("%c", value); // will print 'f' ('e' + 1)
++*ptr
与++(*ptr)
相同,即:
const char *ptr = "example";
char value;
value = *ptr;
++ptr;
printf("%c", value); // will print 'e'
const char *ptr = "example";
char value;
++ptr;
value = *ptr;
printf("%c", value); // will print 'x'
const char *ptr = "example";
char value;
value = *ptr;
++value;
printf("%c", value); // will print 'f' ('e' + 1)
循环中的条件不好:
while(*p++)
printf("%c",*p);
与
while(*p)
{
p++;
printf("%c",*p);
}
这是错误的,这应该是:
while(*p)
{
printf("%c",*p);
p++;
}
*p
*ptr++
*++ptr
++*ptr
*++ptr
int i = 7;
printf ("%d\n", ++i);
printf ("%d\n", i);
8
8
char* p = "Hello";
printf ("%c ", *p); // note space in format string
printf ("%c ", *++p); // value of ++p is p after the increment
printf ("%c ", *p++); // value of p++ is p before the increment
printf ("%c ", *p); // value of p has been incremented as a side effect of p++
H e e l // good dog
char q[] = "Hello";
char* p = q;
printf ("%c", ++*p);
I
(*ptr)++
char q[] = "Hello";
char* p = q;
printf ("%c", (*p)++);
printf ("%c\n", *p);
HI
char q[] = "Hello";
char* p = q;
char* p = "Hello";
printf ("%c", ++*p); // attempting to change string literal!
char p[] = "Hello";
printf ("%c", *++p); // attempting to modify value of array identifier!
*ptr++ // effectively dereferences the pointer, then increments the pointer
*++ptr // effectively increments the pointer, then dereferences the pointer
++*ptr // effectively dereferences the pointer, then increments dereferenced value
(*ptr)++ // effectively forces a dereference, then increments dereferenced value
v = *ptr++
temp = ptr;
ptr = ptr + 1
v = *temp;
v = *++ptr
ptr = ptr + 1
v = *ptr
temp = ptr // Temp created here!!!
ptr = ptr + 1 // or - 1 if decrement)
v = *temp // Temp destroyed here!!!
for (std::set<int>::iterator it = someSet.begin(); it != someSet.end(); it++)
*ptr++ // 1
tmp = *ptr;
ptr++;
*++ptr // 2
++ptr;
tmp = *ptr;
++*ptr // 3
++(*ptr);
const char *p = "Hello";
*p means "Hello"
^
|
p
*p++ means "Hello"
^
|
p
*++p means "Hello"
^
| (WHILE THE STATEMENT IS EXECUTED)
p
*++p means "Hello"
^
| (AFTER THE STATEMENT IS EXECUTED)
p
is "Hello"
^
|
p
const char *p = "Hello";
while('\0')
printf("%c",*p);
const char *p = "Hello";
while(*++p)
printf("%c",*p);
const char *p = "Hello";
while(*p++)
printf("%c",*p);
#include<stdio.h>
int main()
{
int num = 300;
int *ptr;//uninitialized pointer.. must be initialized
ptr = #
printf(" num = %d ptr = %p and data on ptr : %d \n",num,ptr,*ptr);
*ptr = *ptr + 1;//*ptr means value/data on the address.. so here value gets incremented
printf(" num = %d ptr = %p and data on ptr : %d \n",num,ptr,*ptr);
/** observe here that "num" got changed but manually we didn't change, it got modified by pointer **/
ptr = ptr + 1;//ptr means address.. so here address got incremented
/** char pointer gets incremented by 1 bytes
Integer pointer gets incremented by 4 bytes
**/
printf(" num = %d ptr = %p and data on ptr : %d \n",num,ptr,*ptr);
}
#include<stdio.h>
int main()
{
int num = 300;
int *ptr;
ptr = #
printf(" num = %d ptr = %p and data on ptr : %d \n",num,ptr,*ptr);
*ptr++;//address changed(post increment), value remains un-changed
// *++ptr;//address changed(post increment), value remains un-changed
// *(ptr)++;//address changed(post increment), value remains un-changed
// *(++ptr);//address changed(post increment), value remains un-changed
// ++*ptr;//value changed(pre increment), address remains un-changed
// (*ptr)++;//value changed(pre increment), address remains un-changed
// ++(*ptr);//value changed(post increment), address remains un-changed
printf(" num = %d ptr = %p and data on ptr : %d \n",num,ptr,*ptr);
}
#include<stdio.h>
int main()
{
int num = 300;
const int *ptr;//constant value, address is modifible
ptr = #
printf(" num = %d ptr = %p and data on ptr : %d \n",num,ptr,*ptr);
*ptr++;//
// *++ptr;//possible bcz you are trying to change address which is possible
// *(ptr)++;//possible
// *(++ptr);//possible
// ++*ptr;//not possible bcz you trying to change value which is not allowed
// (*ptr)++;//not possible
// ++(*ptr);//not possible
printf(" num = %d ptr = %p and data on ptr : %d \n",num,ptr,*ptr);
}
#include<stdio.h>
int main()
{
int x = 300;
int* const p;
p = &x;
printf("x = %d p =%p and *p = %d\n",num,p,*p);
}
#include<stdio.h>
int main()
{
int x = 300;
/** constant pointer must initialize while decaring itself **/
int* const p;//constant pointer i.e its pointing to some address(here its pointing to garbage), it should point to same address(i.e garbage ad
dress only
p = &x;// but here what we are doing ? we are changing address. we are making p to point to address of x instead of garbage address.
printf("x = %d p =%p and *p = %d\n",num,p,*p);
}
int* const p = &x;
#include<stdio.h>
int main()
{
int num = 300;
int *const ptr = #//constant value, address is modifible
printf(" num = %d ptr = %p and data on ptr : %d \n",num,ptr,*ptr);
*ptr++;//not possible
// *++ptr;//not possible bcz you are trying to change address which is not possible
// *(ptr)++;//not possible
// *(++ptr);//not possible
// ++*ptr;// possible bcz you trying to change value which is allowed
// (*ptr)++;// possible
// ++(*ptr);// possible
printf(" num = %d ptr = %p and data on ptr : %d \n",num,ptr,*ptr);
}
#include<stdio.h>
int main()
{
int num = 300;
const int* const ptr = #//constant value,constant address
printf(" num = %d ptr = %p and data on ptr : %d \n",num,ptr,*ptr);
*ptr++;//not possible
++*ptr;//not possible
printf(" num = %d ptr = %p and data on ptr : %d \n",num,ptr,*ptr);
}