+ 1
why pointers
why this: int* i = &j; cout << *i; but not this: cout << j
2 ответов
+ 1
Hello, Ader !
Your question is very interesting, but please visit the lessons from SoloLearn, then you can understand everything yourself!
Good luck
https://www.sololearn.com/learn/CPlusPlus/1631/?ref=app
https://www.sololearn.com/learn/5926/?ref=app
https://www.sololearn.com/learn/1416/?ref=app
https://www.sololearn.com/learn/5230/?ref=app
0
In the snippet that you have provided, it doesn't matter which one you use to access the value of j. The real difference becomes obvious when, for example, the access to the same variable is desirable from another code block (scope).
~example~
void f(int *p2j) {
// compilation error - j is undefined identifier.
cout << j;
// just indirect access to variable j is possible
cout << *p2j; // 10
}
int main() {
int j = 10;
f(&j);
}