[C++]数组与指针(纯代码-复习用)

#include<iostream>
#include<cmath> //C++
//#include<math.h> //C

#include<ctime>//C++
#include<time.h>//C
#include<cstdlib>//C++
#include<stdlib.h>//C

#include<cstring>
using namespace std;

#define PI 3.141592653

//<cmath>
void math(){

    //double cos(double);
    //该函数返回弧度角(double 型)的余弦。
    
    //double sin(double);
    //该函数返回弧度角(double 型)的正弦。
    
    //double tan(double);
    //该函数返回弧度角(double 型)的正切。
    
    //double log(double);
    //该函数返回参数的自然对数。
    
    //double pow(double, double);
    //假设第一个参数为 x,第二个参数为 y,则该函数返回 x 的 y 次方。
    
    //double hypot(double, double);
    //该函数返回两个参数的平方总和的平方根,也就是说,参数为一个直角三角形的两个直角边,函数会返回斜边的长度。
    
    //double sqrt(double);
    //该函数返回参数的平方根。
    
    //int abs(int);
    //该函数返回整数的绝对值。
    
    //double fabs(double);
    //该函数返回任意一个十进制数的绝对值。
    
    //double floor(double);
    //该函数返回一个小于或等于传入参数的最大整数。即 向下取整 
    
    double num = 2*PI;
    
    cout<<"cos(2*PI):"<<cos(2*PI)<<endl;
    cout<<"sin(2*PI):"<<sin(2*PI)<<endl;
    cout<<"tan(2*PI):"<<tan(2*PI)<<endl;
    cout<<"log(2*PI):"<<log(2*PI)<<endl;
    cout<<"pow(2,3):"<<pow(2,3)<<endl;
    cout<<"hypot(3,4):"<<hypot(3,4)<<endl;
    cout<<"sqrt(2*PI):"<<sqrt(2*PI)<<endl;
    cout<<"abs(2*PI):"<<abs(2*PI)<<endl;
    cout<<"fabs(2*PI):"<<fabs(2*PI)<<endl;
    cout<<"floor(2*PI):"<<floor(2*PI)<<endl;//向下取整 
}
/*
    cos(2*PI):1
    sin(2*PI):-1.17959e-009
    tan(2*PI):-1.17959e-009
    log(2*PI):1.83788
    pow(2,3):8
    hypot(3,4):5
    sqrt(2*PI):2.50663
    abs(2*PI):6.28319
    fabs(2*PI):6.28319
    floor(2*PI):6
*/

//time(arg):<ctime>/<time.h>
//srand():<cstdlib>/<stdlib.h>
void randNum(){
    srand((unsigned)time(NULL));
    for(int i=0;i<10;i++){
        cout<<rand()<<" ";
    }
} 
//test1:106 12839 8211 12377 1817 27409 18868 1179 11303 22190
//test2:576 20513 24747 4086 12555 3034 8308 5635 29676 21604

//array pattern:type name[size1][size2]...[sizeN];
void array(){
    //二维数组
    int dimension2[2][2] = {1,2,3,4};
    for(int i=0;i<2;i++){
        for(int j=0;j<2;j++){
            cout<<dimension2[i][j]<<" ";
        }
        cout<<endl;    
    } 
    //三维数组 
    int magic[3][3][3];
    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            for(int k=0;k<3;k++){
                magic[i][j][k] = i+j+k+3;
                cout<<"magic["<<i<<"]["<<j<<"]["<<k<<"]:"<<(i+j+k+3)<<" ";
            }
            cout<<endl;
        }
        cout<<endl;
    }
}
/*
    1 2
    3 4
    magic[0][0][0]:3 magic[0][0][1]:4 magic[0][0][2]:5
    magic[0][1][0]:4 magic[0][1][1]:5 magic[0][1][2]:6
    magic[0][2][0]:5 magic[0][2][1]:6 magic[0][2][2]:7

    magic[1][0][0]:4 magic[1][0][1]:5 magic[1][0][2]:6
    magic[1][1][0]:5 magic[1][1][1]:6 magic[1][1][2]:7
    magic[1][2][0]:6 magic[1][2][1]:7 magic[1][2][2]:8

    magic[2][0][0]:5 magic[2][0][1]:6 magic[2][0][2]:7
    magic[2][1][0]:6 magic[2][1][1]:7 magic[2][1][2]:8
    magic[2][2][0]:7 magic[2][2][1]:8 magic[2][2][2]:9
*/

//向数组的指针  
void array2(){
    double *p;//指向数组的指针 
    double balance[10] = {10009.0,355,57.56,4546.7,464.45,5,89,85,346}; 
    //balance指向balance[0]的地址;
    //balance =  &balance[0];  

    p = balance;
    cout<<*p<<endl;//10009
    cout<<(*p+2)<<endl;//10011
    cout<<*(p+2)<<endl;//57.56
    cout<<*(p+11)<<endl;//3.65854e-317[error]
}

//传递数组给指针 
void array3(){
    //way 1
    /*形式参数是一个指针:
        void myFunction(int *param){
            ...
        }
    */
    
    //way2
    /*形式参数是一个已定义大小的数组:
        void myFunction(int param[10]){
            ...    
        }
    */
    
    //way3
    /*形式参数是一个未定义大小的数组:
        void myFunction(int param[]){
            ...
        }    
    */
}

//从函数返回数组 
// 要生成和返回随机数的函数
int * getRandom( ){
      static int  r[10];

      // 设置种子
      srand( (unsigned)time( NULL ) );
      for(int i = 0; i < 10; ++i){
        r[i] = rand();
        cout << r[i] << endl;
      }    

      return r;
}
void array4(){
    /*如果您想要从函数返回一个一维数组,您必须声明一个返回指针的函数,如下:
      另外,C++ 不支持在函数外返回局部变量的地址,除非定义局部变量为 static 变量。
        int * myFunction(){
            ...
        }    
    */
        
    // 一个指向整数的指针
       int *p;

       p = getRandom();
       for ( int i = 0; i < 10; i++ ){
       cout << "*(p + " << i << ") : ";
       cout << *(p + i) << endl;
       }
}
/*
7591
6650
25660
16948
17093
16274
9181
9305
8828
21059
*(p + 0) : 7591
*(p + 1) : 6650
*(p + 2) : 25660
*(p + 3) : 16948
*(p + 4) : 17093
*(p + 5) : 16274
*(p + 6) : 9181
*(p + 7) : 9305
*(p + 8) : 8828
*(p + 9) : 21059
*/

//字符串<cstring>/<string.h> 
void string_(){
//    strcpy(s1, s2);
//    复制字符串 s2 到字符串 s1。

//    strcat(s1, s2);
//    连接字符串 s2 到字符串 s1 的末尾。

//    strlen(s1);
//    返回字符串 s1 的长度。

//    strcmp(s1, s2);
//    如果 s1 和 s2 是相同的,则返回 0;如果 s1<s2 则返回小于 0;如果 s1>s2 则返回大于 0。

//    strchr(s1, ch);
//    返回一个指针,指向字符串 s1 中字符 ch 的第一次出现的位置。

//    strstr(s1, s2);
//返回一个指针,指向字符串 s1 中字符串 s2 的第一次出现的位置。

//    char *str1 = "678676";
//    cout<<str1;//678676
//    char *str2 = "hello ";
//    strcpy(str1,str2);//error
//    cout<<strlen(str2)<<endl;//6
    
    char str_a[6] = {'6','7','8','6','7','6'};
    char str_b[5] = {'h','e','l','l','o'};
    strcpy(str_a,str_b);
    cout<<str_a<<str_b;//hellohello
    
}

//指针 
void pointer(){
    int var = 10;//普通变量 
    int *p;//指针变量:指针变量是指存放地址的变量[百度百科]
    //指针变量
    //value:引用值/变量的地址
    //address:指针变量的地址
     
    p = &var;//指针变量赋普通变量的地址值
    
    //空指针
    //在变量声明的时候,如果没有确切的地址可以赋值,为指针变量赋一个 NULL 值是一个良好的编程习惯。
    //赋为 NULL 值的指针被称为空指针。 
    //在大多数的操作系统上,程序不允许访问地址为 0 的内存,因为该内存是操作系统保留的。
    //然而,内存地址 0 有特别重要的意义,它表明该指针不指向一个可访问的内存位置。
    //但按照惯例,如果指针包含空值(零值),则【假定它不指向任何东西】。
    double *ip = NULL;
//    cout<<ip<<" "<<(*ip)<<endl;//error

     
} 

//指针数组
//优先级:()>[]>*
/*
   一个指向字符的指针数组来存储一个字符串列表
*/ 
#define MAX  3 
void pointerArray(){
    //数组指针(也称行指针)
    //定义 int (*p)[n];
    //()优先级高,首先说明p是一个指针,指向一个整型的一维数组,这个一维数组的长度是n,也可以说是p的步长。也就是说执行p+1时,p要跨过n个整型数据的长度。

    //如要将二维数组赋给一指针,应这样赋值:
    int a[3][4];
    int (*p)[4]; //该语句是定义一个数组指针,指向含4个元素的一维数组。
     p = a;        //将该二维数组的首地址赋给p,也就是a[0]或&a[0][0]
     p++;       //该语句执行过后,也就是p=p+1;p跨过行a[0][]指向了行a[1][]

    //所以数组指针也称指向一维数组的指针,亦称行指针。
    
    for(int i = 0;i < 3;i++){
        for(int j=0;j < 4;j++){
            a[i][j] = i+j-10;
        }
    }
    p = a;
    for(int i=0;i<3;i++){
        cout<<*(p[i]+1)<<" "<<p[i]<<endl;//0x70fda0 
    }
    //////////////////////////////////////////////////////////
    int  var[MAX] = {10, 100, 200};
       int *ptr[MAX];//指针数组 (优先级:[]大于*(间接访问))

       for (int i = 0; i < MAX; i++){
        ptr[i] = &var[i]; // 赋值为整数的地址
       }
       for(int i = 0; i < MAX; i++){
        cout << "Value of var[" << i << "] = ";
        cout << *ptr[i] << endl;
       }
       /////////////////////////////////////////////////////////
       
       //优先级:()>[]>*
    int *pointer=NULL;//(普通)指针变量,仅能指向单个地址
    int *poniter2[3];//指针数组变量,指向三个地址
    int (*poniter3)[4];//数组指针变量【行指针】,一个指向行长度为4的数组的指针变量 
} 
/*
-9 0x70fd60
-8 0x70fd70
-7 0x70fd80
Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200
*/

//多级指针(指向指针的指针) 
void pointerToPointer(){
    int var = 100;
    int *p = &var;
    int **pp = &p;//指向指针的指针 
    
    cout<<(&var)<<" "<<p<<" "<<*(pp)<<endl;              //0x70fe0c 0x70fe0c 0x70fe0c
    cout<<(&var)<<" "<<(&p)<<" "<<pp<<" "<<&(pp)<<endl;  //0x70fe0c 0x70fe00 0x70fe00 0x70fdf8
    cout<<var<<" "<<*p<<" "<<*(*pp)<<endl; //100 100 100
}


int main(){
//    math();

//    randNum();

//    array();
//    array2();
//    array3();//传递数组给指针 
//    array4();//从函数返回数组 

//    string_();
    
//    pointerArray();//指针 
    pointerToPointer();//多级指针 
    system("pause");
    return 0;
} 
原文地址:https://www.cnblogs.com/johnnyzen/p/7576361.html