《C++捷径教程》读书笔记--Chapter 6--指针(第一部分)
2005-11-12 23:22
531 查看
//--《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;
}
//--读书笔记--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++捷径教程》读书笔记--Chapter 12--类的深入学习(第一部分)
- 《C++捷径教程》读书笔记--Chapter 5--数组和字符串(第一部分)
- 《C++捷径教程》读书笔记--Chapter 6--指针(第二部分)
- 《C++捷径教程》读书笔记--Chapter 4--程序控制语句(第一部分)
- 《C++捷径教程》读书笔记--Chapter 7--函数,第一部分:基础知识(第一部分)
- 《C++捷径教程》读书笔记--Chapter 9--更多的数据类型与运算符(第一部分)
- 《C++捷径教程》读书笔记--Chapter 11--类(第一部分)
- 《C++捷径教程》读书笔记--Chapter 10--结构与联合
- 《C++捷径教程》读书笔记--Chapter 11--类(第二部分)
- 《C++捷径教程》读书笔记--Chapter 2--C++概述
- 《C++捷径教程》读书笔记--Chapter 11--类(第三部分)
- 《C++捷径教程》读书笔记--Chapter 3--基本数据类型
- 《C++捷径教程》读书笔记--Chapter 5--数组和字符串(第二部分)
- 《C++捷径教程》读书笔记--Chapter 8--函数,第二部分:引用,重载和默认参数
- 《C++捷径教程》读书笔记--Chapter 12--类的深入学习(完结)
- 《C++捷径教程》读书笔记--Chapter 14--继承(完结)
- 《C++捷径教程》读书笔记--Chapter 18--C++的I/O系统--1
- 《C++捷径教程》读书笔记--Chapter 18--C++的I/O系统--2
- 《C++捷径教程》读书笔记--Chapter 18--C++的I/O系统--12-15
- 《C++捷径教程》读书笔记--Chapter 9--更多的数据类型与运算符(第二部分)