C/C++指针 数组

Demo.cpp文件

#include <iostream>
#include <stdio.h>
#include <iomanip>
#include <ctime>

#include "headFile.h"
#include "Object.h"

using namespace std;

int main(int argc,char *argv[])
{
#ifdef X1 // 
	int i=7,j=8;
	printf("i==%d,&i==%p\n",i,&i);
	cout<<"j=="<<j<<" &j=="<<&j<<endl;
#endif
#ifdef X2 // 
	int i=7,j=8;
	int *ip=&i;
	int *jp=&j;
	cout<<"Address: "<<ip<<" contains: "<<*ip<<endl;
	cout<<"Adderss: "<<jp<<" contains: "<<*jp<<endl;
	*ip=9;
	*jp=10;
	cout<<"Now Address: "<<ip<<" contains: "<<*ip<<endl;
	cout<<"Now Adderss: "<<jp<<" contains: "<<*jp<<endl;

#endif
#ifdef X3 // 
	int i=7;
	int *ip=&i;
	int **ipp=&ip;
	cout<<"Address: "<<ip<<" constains: "<<*ip<<endl;
	cout<<"Address: "<<ipp<<" contains: "<<*ipp<<endl;
	cout<<"***ipp== "<<**ipp<<endl;
#endif
#ifdef X4 // 
	float a[]={1.0,2.0,3.0};
	cout<<"sizeof(float)=="<<sizeof(float)<<endl;
	float *p=&a[0];
	cout<<"p=="<<p<<" *p=="<<*p<<endl;
	++p;
	cout<<"p=="<<p<<" *p=="<<*p<<endl;
	ptrdiff_t diff=(p+1)-p;
	cout<<"diff=="<<diff<<endl;
	diff=(char*)(p+1)-(char*)p;
	cout<<"diff=="<<diff<<endl;
#endif
#ifdef X5 // 
	int i=7;
	char *cp=(char*)&i;
	cout<<"The integer at "<<&i<<"=="<<i<<endl;
	for(int n=0;n<sizeof(i);++n)
		cout<<"The byte at"<<(void*)(cp+n)<<"=="<<int(*(cp+n))<<endl;
#endif
#ifdef X6 // 
	unsigned short date,year=92,mon=8,day=2;
	date= (year<<9) | (mon<<5) | day;
	cout<<hex<<date<<endl;
#endif
#ifdef X7 // 
	struct Date
	{
		unsigned day:5;
		unsigned mon:4;
		unsigned year:7;
	};
	
	unsigned short date,year=92,mon=8,day=2;
	Date *dp=(Date*)&date;
	dp->mon=mon;
	dp->day=day;
	dp->year=year;
	cout<<hex<<date<<endl;
	cout<<dec<<"year:"<<int(dp->year)<<" ";
	cout<<dec<<"mon:"<<int(dp->mon)<<" ";
	cout<<dec<<"day:"<<int(dp->day)<<endl;
#endif
#ifdef X8 // 
	int i=7,j=8;
	swap(&i,&j);
	cout<<"i=="<<i<<" j=="<<j<<endl;
#endif
#ifdef X9 //
	char source[]="1234";
	char tar[]="********";
	memcpy(tar,source,strlen(source));
	cout<<source<<" "<<" "<<tar<<endl;
#endif
#ifdef X10 // 
	char c='a';
	short i=100;
	long n=10000L;
	double pi=3.141529;// 40 *9 21 D9 F4 D3 7C 13
	char s[]="hello";
	char az[]="abcdefghijklmnopqrstuvwxyz";// 61~7A
	char AZ[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";// 41~5A
	inspect(&c,sizeof(c));		cout<<endl;
	inspect(&i,sizeof(i));		cout<<endl;
	inspect(&n,sizeof(n));		cout<<endl;
	inspect(&pi,sizeof(pi));	cout<<endl;
	inspect(s,sizeof(s));		cout<<endl;
	inspect(az,sizeof(az));		cout<<endl;
	inspect(AZ,sizeof(AZ));		cout<<endl;
#endif
#ifdef X11 // 
	int a[]={1,2,3,4,5};
	int *p=a;
	cout<<"sizeof(a)=="<<sizeof(a)<<endl;
	cout<<"sizeof(p)=="<<sizeof(p)<<endl;
	cout<<"p=="<<p<<" &a[0]=="<<&a[0]<<endl;
	cout<<"*p=="<<*p<<" a[0]=="<<a[0]<<endl;

	p=a+2;
	cout<<"p=="<<p<<" &a[2]=="<<&a[2]<<endl;
	cout<<"**p=="<<*p<<" a[2]=="<<a[2]<<endl;
#endif
#ifdef X12 // 
	int a[]={1,2,3,4,5};
	size_t n=sizeof(a)/sizeof(a[0]);

	for(int i=0;i<n;++i)
		cout<<a[i]<<' ';
	cout<<endl;

	for(i=0;i<n;++i)
		cout<<i[a]<<' ';
	cout<<endl;

	int* p=a;
	while(p<a+n)
		cout<<*p++<<' ';
	cout<<endl;

	p=a;
	for(i=0;i<n;++i)
		cout<<p[i]<<' ';
	cout<<endl;

	for(i=0;i<n;++i)
		cout<<*(a+i)<<' ';
	cout<<endl;

	p=a+n-1;
	while(p>=a)
		cout<<*p--<<' ';
	cout<<endl;

	p=a+n-1;
	for(i=0;i<n;++i)
		cout<<p[-i]<<' ';
	cout<<endl;
#endif
#ifdef X13 // 匿名数组
	char a[]="hello";
	char *p=a;
	cout<<"a=="<<&a<<" sizeof(a)=="<<sizeof(a)<<endl;
	cout<<"p=="<<(void*)p<<" sizeof(p)=="<<sizeof(p)<<endl;
	cout<<"sizeof\"hello\"=="<<sizeof("hello")<<endl;
	cout<<"address of \"hello\"=="<<(void*)"hello"<<endl;
	cout<<"address of \"hello\"=="<<(void*)"hello"<<endl;
#endif
#ifdef X14 //

	char c[]="0123456789";
	for(int i=0;i<10;i+=2)
		cout<<c[i]<<' ';

	cout<<endl;
	for(i=0;i<10;i+=2)
		cout<<"0123456789"[i]<<' '; // 经典
	cout<<endl;
#endif
#ifdef X15 // 
	int a[]={10,15,4,1,3,-4};
	int *p=&a[2];
	int *q=&a[2];
	cout<<*(p+1)<<endl;
	cout<<p[-1]<<endl;
	cout<<p-a<<endl;
	cout<<a[*p++]<<endl; // *p++ = 4 = a[2]
	cout<<a[*++q]<<endl; // *++q = 1 = a[3]
	cout<<*(a+a[2])<<endl;
#endif
#ifdef X16 // 
	int a[]={0,1,2,3,4};
	size_t n=sizeof(a)/sizeof(a[0]);
	cout<<"a=="<<a<<endl;
	cout<<"sizeof(a)=="<<sizeof(a)<<endl;
	func(a,n);
	for(int i=0;i<n;++i)
		cout<<a[i]<<' ';
	cout<<endl;
#endif
#ifdef X17 // Lincoln实验室 Chet Small 提供的这个聪明的例子
	char s[]="desolate",*p=s;
	cout<<*p++<<endl;
	cout<<*(p++)<<endl;
	cout<<(*p)++<<endl;
	cout<<*++p<<endl;
	cout<<++*p<<endl;
	cout<<++(*p)<<endl;
	//  StrCmp(s,?)==0     ?中 应该是什么 才能满足此表达式
#endif
#ifdef X18 // 指向字符串的指针数组 有个别名叫 “粗糙数组” 
	char* strings[]={"now","is","the","time"};
	size_t n=sizeof(strings)/sizeof(strings[0]);
	for(int i=0;i<n;++i)
		cout<<"string"<<i<<"==\""<<strings[i]
			<<"\"\tsize=="<<sizeof(strings[i])
			<<"\tlength=="<<strlen(strings[i])<<endl;
#endif
#ifdef X19 // 
	char strings[][5]={"now","is","the","time"};
	size_t n=sizeof(strings)/sizeof(strings[0]);
	for(int i=0;i<n;++i)
		cout<<"strings["<<i<<"]==\""<<strings[i]
		<<"\"\tsize=="<<sizeof(strings[i])
			<<"\tlength=="<<strlen(strings[i])<<endl;
#endif
#ifdef X20 // 
	int a[][4]={{0,1,2,3},{4,5,6,7},{8,9,0,1}};
	int (*p)[4]=a;
	size_t nrows=sizeof(a)/sizeof(a[0]);
	size_t ncols=sizeof(a[0])/sizeof(a[0][0]);
	cout<<"sizeof(*p)=="<<sizeof(*p)<<endl;
	for(int i=0;i<nrows;++i)
	{
		for(int j=0;j<ncols;++j)
			cout<<p[i][j]<<' ';
		cout<<endl;
	}
#endif
#ifdef X21 // 
	int a[][3][4]= {
						{
							{0,1,2,3},{4,5,6,7},{8,9,0,1}
						},
						{
							{2,3,4,5},{6,7,8,9},{0,1,2,3}
						}
					};
	int (*p)[3][4]=a;
	size_t ntables=sizeof(a)/sizeof(a[0]);
	size_t nrows=sizeof(a[0])/sizeof(a[0][0]);
	size_t ncols=sizeof(a[0][0])/sizeof(a[0][0][0]);
	cout<<"sizeof(*p)=="<<sizeof(*p)<<endl;
	cout<<"sizeof(a[0][0])=="<<sizeof(a[0][0])<<endl;
	for(int i=0;i<ntables;++i)
	{
		for(int j=0;j<nrows;++j)
		{
			for(int k=0;k<ncols;++k)
				cout<<p[i][j][k]<<' ';
			cout<<endl;
		}
		cout<<endl;
	}
#endif
#ifdef X22 // 
	int (*pFunc)(const char *, ...)=printf;
	pFunc("函数指针的应用\n");
#endif
#ifdef X23 // 
	qsort(argv+1,argc-1,sizeof(argv[0]),comp);
	while(--argc)
		cout<<*++argv<<endl;
#endif
#ifdef X24 //
	if(argc>=2&&argc<=4)
		for(int i=0;i<argc;i++)
			cout<<argv[i]<<endl;
		else 
			cout<<"输入有误"<<endl;

#endif
#ifdef X25 // 
	int i;
	for (i=0; i < argc; i++)
        printf("Argument %d is %s.\n", i, argv[i]);
#endif
#ifdef X26 // 
	extern void retrieve(void);
	extern void insert(void);
	extern void update(void);
	extern int show_menu(int c);
	
	void (*farray[])(void)={retrieve,insert,update}; // 数组元素是函数名字
	for(int choice=0;choice<show_menu(5);++choice)
	{
		farray[choice%3]();	// choice%3 的可能值为 0、1、2 正好可以把farray数组下标覆盖全
	}
#endif
#ifdef X27 // 
	class C
	{
	public:
		void f(){ cout<<"C::f()\n";}
		void g(){ cout<<"C::g()\n";}
	};
	
	C cobject1;
	void (C::*pCF1)()=&C::f;// 未声明直接使用
	(cobject1.*pCF1)();
	pCF1=&C::g;
	(cobject1.*pCF1)();

	cout<<"上下两种风格的类成员函数指针"<<endl;// 上下两种风格

	void (C::*pCF2)();// 声明类的成员函数指针
	C cobject2;
	pCF2=&C::f;
	(cobject2.*pCF2)();
	pCF2=&C::g;
	(cobject2.*pCF2)();
#endif
#ifdef X28 // 
	int show_menu(int);
	srand((int)time(NULL));
	Object O;
	for(;;)
	{
		int choice=show_menu(rand()%100);
		if(0<=choice && choice <3)
			O.process(choice-1);
		else if(choice==4)
			break;
		else
			cout<<"No numbers contains"<<endl;
	}
#endif
#ifdef X29 // 
#endif
#ifdef X3 // 
#endif


	return 0;
}

headFile.h

#ifndef HEADFILE_H
#define HEADFILE_H
#include <iostream>
#include <cstdio>
#include <iomanip>
using namespace std;
#define X28

void swap(int* xp,int* yp);
void* memcpy(void* target,const void* source,size_t n);
void inspect(const void* ptr,size_t nbytes);
void func(int b[],size_t n);
int comp(const void* p1,const void* p2);



#endif

headFile.cpp

#include "headFile.h"


void swap(int* xp,int* yp)
{
	int temp=*xp;
	*xp=*yp;
	*yp=temp;
}

void* memcpy(void* target,const void* source,size_t n)
{
	char * targetp=(char*)target;
	const char* sourcep=(const char*)source;
	while(n--)
		*targetp++=*sourcep++;
	return target;
}

void inspect(const void* ptr,size_t nbytes)
{
	const unsigned char* p=(const unsigned char*)ptr;// 强制转换为const 类型的变量(只为读取不为改写)
	cout.setf(ios::hex,ios::basefield);
	for(int i=0;i<nbytes;++i)
		cout<<"byte"<<setw(2)<<setfill(' ')<<i<<":  "<<setw(2)<<setfill('*')<<int(p[i])<<endl;
}

void func(int b[],size_t n)
{
	cout<<"\n *** Entering function func() *** \n";
	cout<<"b=="<<b<<endl;
	cout<<"sizeof(b)=="<<sizeof(b)<<endl;
	for(int i=0;i<n;++i)
		cout<<b[i]<<' ';
	b[2]=99;
	cout<<"\n *** Leaveing function() ***\n\n";
}

int comp(const void* p1,const void* p2)
{
	const char *ps1=*(const char**)p1;
	const char *ps2=*(char**)p2;
	return strcmp(ps1,ps2);
}

void retrieve(void)	{	printf("1 retrieve\n");	}
void insert(void)	{	printf("2 insert\n");		}
void update(void)	{	printf("3 update\n");		}
int show_menu(int c){int i=c; return i;}

Object.h

#ifndef OBJECT_H
#define OBJECT_H
class Object
{
public:
	void retrieve(void)	{	printf(" Object::retrieve\n");	}
	void insert(void)	{	printf(" Object::insert\n");	}
	void update(void)	{	printf(" Object::update\n");	}
	void process(int choice);
private:
	typedef void (Object::*Omf)();
	static Omf farray[3];
};

Object::Omf Object::farray[3]=	{	&Object::retrieve,
									&Object::insert,
									&Object::update
								};

void Object::process(int choice)
{
	if(0<= choice && choice <=2)
		(this->*farray[choice])();
}
#endif


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值