首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Day 五: Pointers

2012-11-05 
Day 5: Pointers&x evaluates to the adress of x in memory.*(&x) evaluates to the someting as x.?Poin

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;).?

热点排行