需要包含的头文件:
#include "stdafx.h"
#include <iostream.h>
#include <windows.h>
#include <process.h>
1.指针变量加减运算
void main() { int a[8] = {1,2,3,4,5,6,7,8}; int *p; // p = &a[0]; p = a; cout<<"p="<<(int)p<<'\t'<<"*p="<<*p<<endl; p = p+6; cout<<"p="<<(int)p<<'\t'<<"*p="<<*p<<endl; p = p-2; cout<<"p="<<(int)p<<'\t'<<"*p="<<*p<<endl; }
2.指针变量++--运算
void main() { int a[8] = {10,20,30,40,50,60,70,80}; int *p; p=a; p++; cout<<"*p="<<*p<<endl; p=a; cout<<"*p++="<<*p++<<endl; p=a; cout<<"*(p++)="<<*(p++)<<endl; p=a; cout<<"*++p="<<*++p<<endl; p=a; cout<<"(*p)++="<<(*p)++<<endl; p=a; cout<<"++*p="<<++*p<<endl; }
3.结构体类型指针变量
struct person { char name[10]; unsigned age; unsigned long id; float salary; }; void main() { person per1; person *per2=&per1; strcpy(per2->name,"Marry"); per2->age = 24; per2->id = 201121; per2->salary = 2500.0; cout<<per2->name<<'\t'<<per2->age<<'\t'<<per2->id<<'\t'<<per2->salary<<endl; }
4.一维数组与指针
void main() { int a[8]={1,2,3,4,5,6,7,8}; for(int i=0; i<8; i++) { cout<<a[i]<<'\t'; } cout<<endl; for(i=0; i<8; i++) { cout<<*(a+i)<<'\t'; } cout<<endl; int *p; p=a; for(i=0; i<8; i++) { cout<<*p++<<'\t'; } cout<<endl; p=a; for(i=0; i<8; i++) { cout<<*(p+i)<<'\t'; } cout<<endl; p=a; for(i=0; i<8; i++) { cout<<p[i]<<'\t'; } cout<<endl; }
5.二维数组与指针
void main() { int a[4][3]={ {1,2,3}, {4,5,6}, {7,8,9}, {10,11,12} }; int i=0, j=0; int *p; p = &a[0][0]; cout<<"用指针变量输出数组a的所有元素方法一:"<<endl; for(i=0; i<12; i++) { cout<<*p++<<'\t'; if( !( (i+1)%3 ) ) { cout<<endl; } } p=a[0]; cout<<"用指针变量输出数组a的所有元素方法二:"<<endl; for(i=0; i<4; i++) { for(j=0; j<3; j++) { cout<<*(p+i*3+j)<<'\t'; } cout<<endl; } cout<<"用另外的方法输出数组a的所有元素方法一:"<<endl; for(i=0; i<4; i++) { for(j=0; j<3; j++) { cout<<*(a[i]+j)<<'\t'; } cout<<endl; } cout<<"用另外的方法输出数组a的所有元素方法二:"<<endl; for(i=0; i<4; i++) { for(j=0; j<3; j++) { cout<<*(*(a+i)+j)<<'\t'; } cout<<endl; } cout<<"输出数组a的各行第0个元素的地址方法一:"<<endl; for(i=0; i<4; i++) { cout<<&a[i][0]<<'\t'; } cout<<endl; cout<<"输出数组a的各行第0个元素的地址方法二:"<<endl; for(i=0; i<4; i++) { cout<<a[i]<<'\t'; } cout<<endl; cout<<"输出数组a的各行第0个元素的地址方法三:"<<endl; for(i=0; i<4; i++) { cout<<*(a+i)<<'\t'; } cout<<endl; cout<<"输出数组a的各行的行地址方法一:"<<endl; for(i=0; i<4; i++) { cout<<a+i<<'\t'; } cout<<endl; cout<<"输出数组a的各行的行地址方法二:"<<endl; for(i=0; i<4; i++) { cout<<&a[i]<<'\t'; } cout<<endl; }
6.字符指针与一般指针
void main() { int i=2,*p; p=&i; *p=2; cout<<p<<endl; cout<<*p<<endl; char *pCh="China"; cout<<pCh<<endl; cout<<*pCh<<endl; char *pCh1; pCh1 = "NanJing"; pCh1++; cout<<pCh1<<endl; cout<<*pCh1<<endl; }
7.字符指针与字符数组
void main() { char ch[] = "Hello,World!"; char *pCh="NanJing,China!"; char ch1[20], ch2[20],ch3[20]; char *pCh1=ch1, *pCh2=ch2, *pCh3=ch3; //利用循环语句逐字符实现数组的拷贝 for(int i=0; i<strlen(ch)+1; i++) { ch2[i] = ch[i]; } cout<<ch2<<endl; //利用函数实现数组的拷贝 strcpy(ch2,ch); cout<<ch2<<endl; //利用赋值语句将一个字符数组名赋给一个指针变量 pCh1 = ch; cout<<pCh1<<endl; //利用赋值语句将一个指针变量赋给另一个指针变量 pCh1 = pCh; cout<<pCh1<<endl; //利用函数实现字符串的拷贝 strcpy(pCh3,pCh); cout<<pCh3<<endl; //利用函数实现字符串的链接 strcat(pCh3,pCh2); cout<<pCh3<<endl; }
8.指针作为函数参数实现数据交换
void swap(int *p1, int *p2) { int temp; temp = *p1; *p1 = *p2; *p2 = temp; } void main() { int a=2, b=4; int c=20, d=40; int *x=&c, *y=&d; cout<<"交换前:"<<endl; cout<<"a="<<a<<'\t'<<"b="<<b<<endl; cout<<"*x="<<*x<<'\t'<<"*y="<<*y<<endl; swap(&a,&b); swap(x,y); cout<<"交换后:"<<endl; cout<<"a="<<a<<'\t'<<"b="<<b<<endl; cout<<"*x="<<*x<<'\t'<<"*y="<<*y<<endl; }
9.指针作为函数参数实现数据交换
void swap(int &x,int &y) { int temp; temp = x; x = y; y = temp; } void main() { int a=2, b=4; cout<<"a="<<a<<'\t'<<"b="<<b<<endl; swap(a,b); cout<<"a="<<a<<'\t'<<"b="<<b<<endl; }
10.字符串排序
void sort1(char *pCh[], int n) { char *pCh1; int k; for(int i=0; i<n-1; i++) { k=i; for(int j=i+1; j<n; j++) { if( strcmp(pCh[k],pCh[j]) > 0 ) k=j; } if(i != k) { pCh1 = pCh[i]; pCh[i] = pCh[k]; pCh[k] = pCh1; } } } void sort2(char **pCh, int n) { char *pCh1; for(int i=0; i<n-1; i++) { for(int j=0; j<n; j++) { if( strcmp(pCh[i],pCh[j]) >0 ) { pCh1 = pCh[i]; pCh[i] = pCh[j]; pCh[j] = pCh1; } } } } void main() { char *pCh[] = {"NanJing","BeiJing","WuHan","ShengYang"}; sort1(pCh,4); for(int i=0; i<4; i++) { cout<<pCh[i]<<'\t'; } cout<<endl; char *pCh1[] = {"JiangSu","SiChuan","ShanDong","LiaoNing","HeBei"}; sort2(pCh1,5); for(i=0; i<5; i++) { cout<<pCh1[i]<<'\t'; } cout<<endl; }
11.加减乘除四则运算
float add(float x, float y) { return x+y; } float sub(float x, float y) { return x-y; } float mul(float x, float y) { return x*y; } float dev(float x, float y) { if(y != 0) return x/y; else { cout<<"除数不能为0!"<<endl; exit(1); } } void main() { float a,b; char c; float (*pf)(float,float); cout<<"输入计算式,格式为:操作数1,运算符,操作数2"<<endl; cin>>a>>c>>b; switch(c) { case '+': pf = add; break; case '-': pf = sub; break; case '*': pf = mul; break; case '/': pf = dev; break; default: cout<<"输入数据格式不对!"<<endl; exit(1); break; } cout<<a<<c<<b<<"="<<pf(a,b)<<endl; }
12.引用类型的变量
void fun() { int i=10; static int &j=i; j++; cout<<j<<endl; } void main() { float k, &ref=k; k=100.0; cout<<"&k:"<<&k<<"\t&ref:"<<&ref<<endl; cout<<"k="<<k<<"\tref="<<ref<<endl; ref += 200; cout<<"k="<<k<<"\tref="<<ref<<endl; float l=500.0; ref=l; cout<<"&k:"<<&k<<"\t&ref:"<<&ref<<"\t&l:"<<&l<<endl; cout<<"k="<<k<<"\tref="<<ref<<"\tl="<<l<<endl; fun(); }
13.通过指针变量访问整型变量
void main() { int a; int b; int *p1; int *p2; a=100; b=80; p1=&a; p2=&b; cout<<"p1="<<*p1<<endl; cout<<"p2="<<*p2<<endl; p2=p1; cout<<"p2="<<*p2<<endl; }
14.输入两个整数,按先大后小顺序输出(改变指针所指向的变量)
void main() { int *p, *p1, *p2, a, b; cout<<"please input two numbers:"<<endl; cin>>a; cin>>b; p1=&a; p2=&b; if(a<b) { p=p1;p1=p2;p2=p; } cout<<"a="<<a<<endl; cout<<"b="<<b<<endl; cout<<"max="<<*p1<<endl; cout<<"min="<<*p2<<endl; }
15.指针变量作为函数参数(改变a和b的值)
int swap(int *p1, int *p2) { int temp; temp = *p1; *p1 = *p2; *p2 = temp; return 0; } void main() { int a, b; int *p1, *p2; cout<<"please input two numbers:"<<endl; cin>>a; cin>>b; p1=&a; p2=&b; if(a<b) { swap(p1, p2); } cout<<"max="<<a<<endl; cout<<"min="<<b<<endl; }