c++学习笔记

指针作为函数参数:

(可以实现数值的交换)

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值