指针作为函数参数:
(可以实现数值的交换)
#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
int main()
{
void swap(int *p1,int *p2);
int *pointer_1,*pointer_2,a,b;
cin>>a>>b;
pointer_1 = &a;
pointer_2 = &b;
if(a<b)
swap(pointer_1,pointer_2);//或者swap(&a,&b);
cout<<"max ="<<a<<"min="<<b<<endl;
return 0;
}
void swap(int *p1,int *p2)
{
int temp;
temp = *p1;
*p1 = *p2;
*p2 = temp;
}
-------------------------------------------正经的分割线--------------------------------------------------------------
(不能实现数值的交换)
#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
int main()
{
void swap(int *p1,int *p2);
int *pointer_1,*pointer_2,a,b;
cin>>a>>b;
pointer_1 = &a;
pointer_2 = &b;
if(a<b)
swap(pointer_1,pointer_2);
cout<<"max ="<<a<<"min="<<b<<endl;
cout<<"max ="<<*pointer_1<<"min="<<*pointer_2<<endl;
return 0;
}
void swap(int *p1,int *p2)
{
int *temp;
temp = p1;
p1 = p2;
p2 = temp;
}
-------------------------------------------正经的分割线--------------------------------------------------------------
/**
若干字符串按照字母顺序(由小到大)输出*/
#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
int main()
{
void sort(char *name[],int n);//指针数组
void print(char *name[],int n);
char *name[] = {"basic","fortran","c++","pascal","cobol"};
int n = 5;
sort(name,n);
print(name,n);
return 0;
}
void sort(char *name[],int n)
{
char *temp;
int i,j,k;
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
if(strcmp(name[k],name[j])>0)
k = j;
if(k!=i)
{
temp = name[i];
name[i] = name[k];
name[k] = temp;
}
}
}
void print(char *name[],int n)
{
int i;
for(i = 0;i<n;i++)
cout<<name[i]<<endl;
}
----------------------------------------------------------------------------------------------
/**
将字符串str1复制为str2*/
#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
int main()
{
char str1[] = " i love china !",str2[20],*p1,*p2;
p1 = str1;p2 = str2;
for(;*p1 = '\0';p1++,p2++)
*p2 = *p1;
*p2 = '\0';
p1 = str1;p2 = str2;
cout<<"str1 is:"<<p1<<endl;
cout<<"str2 is:"<<p2<<endl;
return 0;
}