Day 5: Pointers
&x evaluates to the adress of x in memory.*(&x) evaluates to the someting as x.
Pointer advantages:
[*]More flexible pass-by-reference
[*]Manipulate comlex data structure efficiently
[*]Use polymorphism
Pointers are variables stroring memory address.
Declaring pointers:
int *ptr = &x;
As with any other variable, the value of a pointer is undefined until it is initialized , so it may be invalid.
References:
int y;int &x = y;Reference variable x becomes another name for the value of y in memory.
The usage of the * and & operators with pointers/references can be confusing. The * operator
is used in two different ways:
[*]When declaring a pointer, * is placed before the variable name to indicate that the variable being declared is a pointer – say, a pointer to an int or char, not an int or char value.
[*]When using a pointer that has been set to point to some value, * is placed before the pointer name to dereference it – to access or set the value it points to.
A similar distinction exists for &, which can be used either
[*] to indicate a reference data type (as in int &x;), or
[*] to take the address of a variable (as in int *ptr = &x;).
页:
[1]