vc++ 的指针和数组结合理解的最佳例子

来源:互联网 发布:mysql 分表 编辑:程序博客网 时间:2024/06/06 07:23

下面的方法先接受用户的输入(不超过80个字符),然后计算输入的字符的长度:


#include "stdafx.h"#include <iostream>#include <iomanip>#include <Windows.h>using std::cout;using std::cin;using std::endl;using std::setw;int main(int argc,_TCHAR* argv[]){const int MAX(80);char buffer[MAX];char* pbuffer(buffer);cout<<endl<<"Enter a string of less than "<<MAX<<" characters:"<<endl;cin.getline(buffer,MAX,'\n');while(*pbuffer++); //这一行是关键,是精髓!cout<<endl<<"The string \"" <<buffer<<"\" has "<<pbuffer - buffer -1 <<" characters.";cout<<endl;return 0;}

输出结果如下:

Enter a string of less than 80 characters:asdfasdfasdfasdfThe string "asdfasdfasdfasdf" has 16 characters.请按任意键继续. . .



0 0
原创粉丝点击