读书人

Day 五: Pointers

发布时间: 2012-11-05 09:35:12 作者: rapoo

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-referenceManipulate comlex data structure efficientlyUse 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 ?di?erent ?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;).?

读书人网 >编程

热点推荐