+ 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 odpowiedzi
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
OSZAR »