+ 1

why pointers

why this: int* i = &j; cout << *i; but not this: cout << j

28th Jun 2018, 10:19 AM
Ader
Ader - avatar
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
28th Jun 2018, 10:20 AM
Alexander Sokolov
Alexander Sokolov - avatar
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); }
28th Jun 2018, 11:11 AM
To Seek Glory in Battle is Glorious
To Seek Glory in Battle is Glorious - avatar