美文网首页
C++ 指针

C++ 指针

作者: __method__ | 来源:发表于2021-04-24 09:08 被阅读0次

指针的基本概念

指针的作用: 可以通过指针间接访问内存

  • 内存编号是从0开始记录的,一般用十六进制数字表示
  • 可以利用指针变量保存地址
    指针变量的定义和使用
    指针变量定义语法: 数据类型 * 变量名;
int main() {

    //1、指针的定义
    int a = 10; //定义整型变量a
    
    //指针定义语法: 数据类型 * 变量名 ;
    int * p;

    //指针变量赋值
    p = &a; //指针指向变量a的地址
    cout << &a << endl; //打印数据a的地址
    cout << p << endl;  //打印指针变量p

    //2、指针的使用
    //通过*操作指针变量指向的内存
    cout << "*p = " << *p << endl;

    system("pause");

    return 0;
}

指针变量和普通变量的区别

  • 普通变量存放的是数据,指针变量存放的是地址
  • 指针变量可以通过" * "操作符,操作指针变量指向的内存空间,这个过程称为解引用

总结1: 我们可以通过 & 符号 获取变量的地址

总结2:利用指针可以记录地址

总结3:对指针变量解引用,可以操作指针指向的内存

1

#include <iostream>
using namespace std;
int main( )
{
    char c = 'A' ; 
    char *cp = &c ;         // A

    cout << c << *cp << ',' ; 
    c = 'B' ; 
    cout << c << *cp << ','  ; 
    *cp = 'a' ;
    cout << c << *cp << endl ; 
}

2

#include <iostream>
using namespace std;
int main( )
{
    int a=1, *p1;   
    float b=5.2, *p2;  
    char c='A', *p3; 
    
    p1 = &a;  
    p2 = &b;     
    p3 = &c;
    
    cout << a << ',' << b << ',' << c << endl; 
    cout << *p1 << ','<< *p2 << ',' << *p3 << endl; 
    *p1 = *p1+1;  // *p1等价于a
    *p2 = *p2+2;  // *p2等价于b
    *p3 = *p3+3;  // *p3等价于c
    cout << a << ',' << b << ',' << c << endl;
} 

3 指针变量交换

#include <iostream>
using namespace std;
int main( )
{
   int x = 10 , y = 20 ; 
   int *p1 = &x ,*p2 = &y, *t ;

   cout << *p1 << '\t' << *p2 << endl ;
   t = p1;  p1 = p2 ;  p2 = t ;                // A
   cout << *p1 << '\t' << *p2 << endl;
}

#include <iostream>
using namespace std;
int main( )
{
   int x = 10 , y = 20 ; 
   int *p1=&x ,*p2 = &y, t ;

   cout << x << '\t' << y << endl ;
   t = *p1;  *p1 = *p2 ;  *p2 = t ;     // A
   cout << x << '\t' << y << endl ; 
}

4

#include <iostream>
using namespace std;
int main( )
{
    int a, *p1;  
    float b, *p2;  
    double d, *p3;

    p1=&a;    
    p2=&b;   
    p3=&d;  
    cout << "p1=" << p1 << endl;
    cout << "p2=" << p2 << endl;
    cout << "p3=" << p3 << endl;
}

5 swap

#include <iostream>
using namespace std;

void swap(int x, int y)
{
    int t;

    t = x ;  x = y ;  y = t ;
}
int main( )
{
    int  x=3, y=9;

    swap(x, y);
    cout<< x<< ',' << y <<endl; 
}


指针作为参数

#include <iostream>
using namespace std;
void swap(int *px, int *py)
{ 
   int t;

   t = *px ; *px = *py ; *py = t;
}
int main( )

{ 
   int x = 3, y = 9, *p1, *p2;

   p1 = &x; p2 = &y;
   swap (p1, p2);
   cout<< x<<endl;
   cout<< y<<endl;
}

6

#include <iostream>
using namespace std;

void lowterm(int *num , int *den) 
{ 
    int n , d , r ;

    n = *num ;              // *num间接访问a 
    d = *den ;              // *den 间接访问b 
    while(d != 0 )          // 用辗转相除法,求分子、分母的最大公约数
    { r = n%d ; n = d ; d = r ; }
    if(n>1)                 // 当最大公约数n大于1时,用n除分子、分母
    { 
       *num = *num/n ; 
        *den = *den/n ; 
    } 
}
int main( )

{
    int a = 14, b = 21;         // a是分子,b是分母

    cout << "分数:" << a << '/' << b << endl ;    // 输出分数
    lowterm(&a, &b) ;
    cout << "约简后:"<< a << '/' << b << endl ;    // 输出约简后的分数
}

7 一维数组与指针

#include <iostream>
using namespace std;
int main( )

{ 
    int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, *p = a, sum = 0, i; 

    for( i=0; i<10; i++)    
        sum = sum + *(p+i);
    for( i=0; i<10; i++, p++)   //
        cout << *p<<'\t';
    cout << endl << "sum=" << sum << endl;
}


2

#include <iostream>
using namespace std;
int main( )
{
    int a[10]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, *p, sum=0 ;

    for(p = a ; p < a+10; p++)  // 此循环结束后,p指向a[9]之后的存储空间
        sum = sum + *p ;
    for(p = a; p < a+10; p++)   // 此循环初始时,需重新给p赋初值
        cout << *p<<'\t';
    cout  << endl << "sum=" << sum << endl;
}

利用指针反转数组

#include <iostream>
using namespace std;
int main( )
{
    int a[10]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, t;
    // 首地址
    int *p1=a, *p2=a+9 ; 

    while(p1<p2)
    {
        t = *p1; *p1 = *p2; *p2=t; 
        p1++; p2 -- ;
    }
    for(p1 = a; p1 < a+10; p1++)  
        cout << *p1 << "\t";
    cout << endl;
}
using namespace std;
void reverse( int b[ ], int n )
{
    int i=0, j= n-1, t; 

    while(i<j ) 
    { 
        t=b[i]; b[i]=b[j]; b[j]=t; 
        i++; j--;
    }
}
int main(void)
{  
    int a[10]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, i; 
    
    reverse(a, 10);
    for(i=0; i<10; i++) cout << a[i] << "\t ";
    cout << endl;
}

逆序

void reverse( int b[], int n )
{
    int  t;
    int *p2 = b+n-1;

    while( b < p2 )
    {
        t = *b; *b = *p2; *p2=t;
        b++; p2 --;
    }
}

int  main()
{
    int  a[10]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, i ;

    reverse(a+3, 6);
    for(i=0; i<10; i++)
        cout << a[i] << "\t" ;
    cout << endl ;
}

部分逆序存放

// 部分逆序存放
void reverse( int b[], int n )
{
    int  t;
    int *p2 = b+n-1;

    while( b < p2 )
    {
        t = *b; *b = *p2; *p2=t;
        b++; p2 --;
    }
}
int  main()
{
    int  a[10]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, i ;
    reverse(a+3, 6);
    for(i=0; i<10; i++)
        cout << a[i] << "\t" ;
    cout << endl ;
}

求前十和 和后10 和

#include <iostream>
using namespace std;
int fsum(int *array, int n)   // 通用的求和函数
{
    int i, s=0; 
    
    for(i=0; i<n; i++) 
        s+=array[i]; 
    return(s); 
}
int main( )

{
    int a[15]={ 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 }; 
    int shead, stail; 

    shead = fsum(a, 10);        // 第1次调用   55
    stail = fsum(&a[5], 10);    // 第2次调用   6,7,8,9,10,11,12,13,14,15
    cout << shead << ", " << stail << endl ;
}

数组最大值和最小值

#include <iostream>
using namespace std;
void max_min_value (int *array, int n, int *maxp, int *minp)
{   //maxp指向主函数max变量,minp指向主函数min变量
    int *p, *array_end;
    
    array_end = array + n ;
    *maxp=*minp=*array ;    // 假定第0个元素既是最大值又是最小值
    for(p = array+1; p < array_end; p++)
    {
        if( *p>*maxp ) *maxp=*p;
        else if( *p<*minp ) *minp=*p; 
    }
}
int main( )
{ 
    int i, number[10], *p = number, max, min;
    
    for(i = 0; i < 10; i++)
        cin >> *( p+i );            // 输入数组元素值
    max_min_value( p, 10, &max, &min );
    for(i = 0; i < 10; i++)  
        cout << *( p+i ) << "\t" ;  // 输出数组元素值
    cout << "max value = " << max << "min value = " << min << endl ;
}

t通过指针访问数组元素, 实现选择排序法

#include <iostream>
using namespace std;
void sortd(int *a, int n) 
{  
    int *p, *q, *maxp, t;
    
    for( p = a; p < a + n -1; p++)
    {
        maxp = p;
        for( q = p+1; q < a + n; q++)
            if( *q > *maxp ) maxp = q;   // 排降序
        if( maxp != p )
        { t= *p; *p=*maxp; *maxp=t; }
    }
}
int main( )
{ 
    int i, a[10];
    
    for(i = 0; i < 10; i++)
        cin >> *( a+i );  // 输入数组元素值
    sortd( a, 10 );
    for(i = 0; i < 10; i++)  
        cout << *( a+i ) << '\t';  // 输出排序后的数组元素值
    cout<<endl;
}

字符串追加

#include <iostream>
#include <cstring>
using namespace std;
int main( )
{
    char a[20] = "ABCD", b[10] = "EFG";
    int  i, j;

    i = strlen(a);  // i是字符串a的长度,也是 '\0' 字符的下标
    for(j = 0; b[j] != '\0'; i++, j++)
        a[i] = b[j];
    a[i] = '\0';
    cout << a << endl ;
}

//解2:用字符指针访问方式实现

 
int main( )
{
    char a[20] = "ABCD", b[10] = "EFG";
    char *pa = a, *pb = b;      // pa指向a[0],pb指向b[0]

    while(*pa != '\0') pa++;    // 循环结束后,pa指向a 尾部的'\0'
    while(*pb != '\0')
    {
        *pa = *pb;
        pa++; pb++;
    }
    *pa = '\0';
    pa = a;
    cout << pa << endl;     // 请注意输出字符指针的意义
}

相关文章

  • C++知识点

    C++基本方法: C++ memcpy C++基本特性: C++引用(vs指针) C++指针 C++封装: 将...

  • C++ 指针常量、常量指针和常指针常量

    参考:C++ 指针常量、常量指针和常指针常量

  • Java基础

    Java和C++的区别?a. Java没有指针。c++可以通过指针直接操作内存,但这个动作是危险的,指针引起的操作...

  • Jna send pointer pointer to c++

    目的: 有这样一个需求,java通过jna传递指针数组给c++,或者指针的指针 解决方案: c++ : 声明 vo...

  • C++ 指向类的指针

    原文地址:C++ 指向类的指针 一个指向 C++ 类的指针与指向结构的指针类似,访问指向类的指针的成员,需要使用成...

  • C++基础

    C++ 值传递、指针传递、引用传递详解C++中引用传递与指针传递区别 引用传递和指针传递的区别 引用的规则:(1)...

  • C++函数指针和Swift的函数对象

    C++函数指针和Swift的函数对象 在C++中学习函数指针式非常痛苦的事情,而在Swift里面学习函数指针则是非...

  • [C++之旅] 16 对象指针/对象成员指针/this指针

    [C++之旅] 16 对象指针/对象成员指针/this指针 一、对象指针 对象指针指向堆中地址 对象指针指向栈中地...

  • C++ 、java 和 C# 的区别

    一、基础类型 c++: ** java:** C#: 二、指针 1.java 是没有指针这个概念,c++ 和 c#...

  • 静心学习之路(7)——C++干架用知识

    善用书籍后自带的单词索引 指针、引用、数组、内存。《C++ Primer 5th》2.3.2——指针《C++ Pr...

网友评论

      本文标题:C++ 指针

      本文链接:https://www.haomeiwen.com/subject/ziskrltx.html