指针初步学习(C++)

pointer:
1).指向const对象的指针:C++强制要求指向const对象的指针也必须具有const特性;定义时不需要对其进行初始化。允许对其重新赋值,使其指向另一个const对象,但不能通过它修改所指对象的值
2).把一个const对象地址赋给一个普通的,非const对象的地址会导致编译错误,不能通过
3).必须使用const *void类型的指针保存const对象的地址
Example:
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
 //const double *cptr;  //cptr may point to a double that is const
 //*cptr=42; //error: *cptr might be const
 const double pi=3.14;
 //cptr=&pi;
 //double *ptr=&pi; //error:pi is a plain pointer
 const double *cptr=&pi;
 cout<<*cptr<<endl;

 const int universe=42;
 const void *cpv=&universe;  //ok:cpv is const
 //void *cpv=*universe;   //error:univers is const
 cout<<&cpv<<endl;

 double dval=3.14159267;  //dval is a double;its value can be changed
 cptr=&dval;    //ok:but can't change dval through cptr
 cout<<*cptr<<endl;

 dval=3.14159;  //dval is not const
 //*cptr=3.14159;  //error:cptr is a pointer to a const
 double *ptr=&dval;  //ok:ptr points at non-const double
 *ptr=2.72;   //ok:ptr is plain pointer
 cout<<*ptr<<endl;
 return 0;
}
4).const指针(本身的值不能修改):定义时初始化,不能使其指向其他对象,但能否修改所指对象的值完全取决于该对象的类型,只要不是常量便可以修改
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
 int errNumb=1;
 int *const curErr=&errNumb;  //curErr is a constant pointer
 cout<<*curErr<<endl;
 //curErr=curErr;     //curErr is a constant pointer
 

 if(*curErr)
 {
  //errorHandler();
  *curErr=2;      //reset value of the object to which curErr is bound
  cout<<*curErr<<endl;
 }
 cout<<*curErr<<endl;
 return 0;
}

5).指向const对象的const指针
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
 const double pi=3.14159276;
 //pi_ptr is const and points to a const object
 const double *const pi_ptr=&pi;
 cout<<*pi_ptr<<endl;

 return 0;
}
6).C风格字符串(永远不要忘记字符串结束符null)
7).调用者必须确保目标字符串具有足够大小
8).尽可能使用标准库类型string
#include "stdafx.h"
#include <iostream>
#include <cstring>
#include <string>
using namespace std;

int main()
{
 const char *cp1="A string example";
 const char *cp2="A different string";
 int i=strcmp(cp1,cp2);    //i is positive
 cout<<i<<endl;
 i=strcmp(cp2,cp1);     //i is negative
 cout<<i<<endl;
 i=strcmp(cp1,cp1);     //i is zero
 cout<<i<<endl;

 char ca[]={'C','+','+'};   //not null-terminated
 cout<<strlen(ca)<<endl;    //disaster:ca isn't null-terminated

 //Dangerous:what happens if we miscalculate the size of largeStr?
 char largeStr[16+18+2];   //will hold cp1 a space and cp2
 strcpy(largeStr,cp1);   //copies cp1 into largeStr
 strcat(largeStr," ");   //adds a space at end of largeStr
 strcat(largeStr,cp2);   //concatenates cp2 to largeStr
 //prints A string example A different string
 cout<<largeStr<<endl;

 char largeStr1[16+18+2];   //to hold cp1 a space and cp2
 strncpy(largeStr,cp1,17);  //size to copy includes the null
 strncat(largeStr," ",2);  //pedantic,but a good habit
 strncat(largeStr,cp2,19);  //adds at most 18 characters,plus a null

 cout<<largeStr1<<endl;

 string largeStr2=cp1;  //initialize largeStr2 are a copy of cp1
 largeStr2+=" ";    //add space at end of largeStr2
 largeStr2+=cp2;    //concatenate cp2 onto end of largeStr2;
 cout<<largeStr2<<endl;

 return 0;
}
9).比较两个string类型的字符串
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;

int main()
{
 string str1,str2;
 //输入两个字符串
 cout<<"Enter two string: "<<endl;
 cin>>str1>>str2;
 //比较两个字符串
 if(str1>str2)
  cout<<"/""<<str1<<" is bigger than "
  <<"/""<<str2<<"/""<<endl;
 else if(str1<str2)
  cout<<"/""<<str2<<" is bigger than "
  <<"/""<<str1<<endl;
 else
  cout<<"They are equal"<<endl;
 return 0;

}
10).比较两个C风格字符串
#include "stdafx.h"
#include <cstring>
#include <iostream>
using namespace std;

int main()
{
 const int str_size=80;
 char *str1,*str2;
 //为两个字符串分配内存
 str1=new char[str_size];
 str2=new char[str_size];
 if(str1==NULL||str2==NULL)
 {
  cout<<"No enough memory!"<<endl;
  return -1;
 }

 //输入两个字符串
 cout<<"Enter two string: "<<endl;
 cin>>str1>>str2;
 int result;
 result=strcmp(str1,str2);
 if(result>0)
  cout<<"/""<<str1<<"/""<<" is bigger than "
   <<"/""<<str2<<"/""<<endl;
 else if(result<0)
  cout<<"/""<<str2<<"/""<<" is bigger than "
   <<"/""<<str1<<"/""<<endl;
 else
  cout<<"They are equal"<<endl;

 //释放字符串所占用的内存
 delete []str1;
 delete []str2;

 return 0;
}

11).从数组ca的起始地址开始,输出一段内存中存放的字符,每行输出一个字符,直至存放0值(null)的字节为止
#include "stdafx.h"
#include <iostream>
#include <cstring>
#include <string>
using namespace std;

int main()
{
 const char *cp="hello";
 int cnt;
 while(*cp)
 {
  cout<<cp<<endl;
  ++cnt;
  ++cp;
 }
 cout<<endl;

 const char ca[]={'h','e','l','l','o'};
 const char *cp1=ca;
 while(*cp1)
 {
  cout<<*cp1<<endl;
  ++cp1;
 }
 return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值