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;). 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值