+ 2
Why is pointer not updating the variable?
This code is giving output '101100'. I want to know why ++*p in 'cout' statement not updating the value of 'a' in the program. When I write '++*p' outside the 'cout' it updates the variable 'a'. I have also tried this code in c and it gives the same output. https://code.sololearn.com/c8nr9k9ZGCt0/?ref=app
3 Antworten
+ 2
cout>>++*p >> *p;
++*p ==> another word: *p=*p+1
it will not implement assign operator ultil meet ';'
Because assign operator is valid after ';'.
the result of pression (++*p) is as same as (*p+1).
*p value can't change before ';'
Try this code to understand:
#include <iostream>
using namespace std;
int main()
{
int a=10;
int *p=&a;
cout<<++*p<<*p;
cout<<*p;
return 0;
}
//result: 11 10 11
+ 7
it is updating.
using multiple << with ++ and cout it acts strange
use two cout statements and you will see it is correct
cout<<++*p;
cout<<*p;
😆
0
@jay thanks for that but I want to know what happens when I write it together.Why doesn't the *p prints updated value?