//--《C++捷径教程》读书笔记--Chapter 6--指针(第一部分)
//--读书笔记--Chapter 6--指针
//--11/12/2005 Sat.
//--Computer Lab
//--Liwei
//--程序#1
#include <iostream>
using namespace std;
int main()
{
int balance;
int *balptr;
int value;
balance=3200;
balptr=&balance;
cout<<balptr<<endl;
value=*balptr++;//注意这样的使用
cout<<"balance is: "<<value<<'/n'<<balptr<<' '<<*balptr<<endl;
return 0;
}
//--程序#2
#include <iostream>
using namespace std;
int main()
{
double x,y;
int *p;
x=123.23;
p=(int *)&x;//将double类型的指针转换为 int类型
y=*p;
cout<<y<<' '<<*p<<endl;
return 0;
}
//--程序#3 通过指针赋值
#include <iostream>
using namespace std;
int main()
{
int *p,num;
p=#
*p=100;
cout<<num<<' ';
(*p)++;
cout<<num<<' ';
(*p)--;
cout<<num<<'/n';
return 0;
}
//--程序#4 说明指针运算
#include <iostream>
using namespace std;
int main()
{
char *c,d[10];
wchar_t *cc,dd[10];
int *i,j[10];
float *m,n[10];
double *f,g[10];
int x;
c=d;
cc=dd;
i=j;
m=n;
f=g;
cout<<"===char==="<<"wchar_t==="<<"int==="<<"float==="<<"double==="<<'/n';
for(x=0;x<10;x++)
cout<<(int)c+x<<' '<<cc+x<<' '<<i+x<<' '<<m+x<<' '<<f+x<<'/n';//这里记得 (int)c 但是不可(int)cc
return 0;
}
//--程序#5 提取记号程序:指针版本
#include <iostream>
using namespace std;
int main()
{
char str[40];
char token[80];
char *p,*q;
cout<<"Enter a sentence: ";
gets(str);
p=str;
while(*p)//每次从字符串中提取一个字符,直到 *p='/0'
{
q=token;//将q指向一个记号的开始
while(*p!=' '&& *p) // && *p
*q++=*p++; //q++; p++;
if(*p) p++;//跳过空格, *p=' '时
*q='/0';//以空字符结束标记
cout<<token<<"/n================"<<endl;
}
return 0;
}
//--程序#6 提取记号程序:数组下标版本
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
char str[40];
char token[80];
int i,j;
cout<<"Enter a sentence: ";
gets(str);
for(i=0; ; i++)
{
for(j=0; str[i]!=' '&&str[i]; j++,i++ )
token[j]=str[i];
token[j]='/0';
cout<<token<<"======"<<endl;
if(!str[i])
break;
}
return 0;
}
//--程序#7 象数组一样对指针使用下标
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char str[20]="hello tom";
char *p;
int i;
p=str;
for(i=0;p[i];i++)//注意这里的 p[i]
p[i]=toupper(p[i]);
cout<<p<<endl;
return 0;
}
这是《C++捷径教程》第六章指针部分的读书笔记,包含多个C++程序示例。涉及指针的定义、使用、类型转换、运算,以及通过指针赋值等操作,还展示了提取记号程序的指针版本和数组下标版本,以及像数组一样对指针使用下标的示例。

被折叠的 条评论
为什么被折叠?



