zoukankan      html  css  js  c++  java
  • 指针(pointer)的使用

    1、变量指针指针变量

    变量的指针:变量的存储地址,

    指针变量:存储指针的变量

    2、指针变量的基本符号

    &:取地址符号

    *:间接取址符号

    NULL||0:初始化

    **:多级指针

    *p[]:指针数组

    3、程序实例(自己 尝试写类函数,一直不懂啥时候才定义变量位为private

     1 //*m:定义指针变量  &m:取m的地址 *&m:间接访问 m的内容
     2 //&和 *互为逆运算
     3 
     4 #include <iostream>
     5 #include<stdlib.h>
     6 using namespace std;
     7 /*void main(){
     8     int *x, *y;
     9     int m, n;
    10     cout << "请输入m和n" << endl;
    11     cin >> m >> n ;
    12     x = &m;
    13     y = &n;
    14     cout << *x << " " << *y << endl;
    15     int *temp;
    16     temp = x;
    17     x = y;
    18     y = temp;
    19     cout << *x << " " << *y << endl;
    20 
    21 }*/
    22 class pointer{
    23 public:
    24     void _cinvlauemn();
    25     void _cinvaluea();
    26     void _swappointer();
    27     void _pointerarray();
    28 private:
    29     int m, n;
    30     int a[5];
    31 };//一定要记得输入分号
    32 void pointer::_cinvlauemn(){
    33     cout << "请输入m和n" << endl;
    34     cin >> m >> n;
    35 }
    36 void pointer::_cinvaluea(){
    37     cout << "请输入数组值: " << endl;
    38     for (int i = 0; i < sizeof(a) / sizeof(int); i++){
    39         cin >> a[i];
    40     }
    41 }
    42 void pointer::_swappointer(){
    43     int *x=NULL, *y=0;//初始化
    44     x = &m;
    45     y = &n;
    46     cout << *x << " " << *y << endl;
    47     int *temp;
    48     temp = x;
    49     x = y;
    50     y = temp;
    51     cout << *x << " " << *y << endl;
    52 }
    53 void pointer::_pointerarray(){
    54     int *p[5];
    55     for (int i = 0; i < 5; i++){
    56         p[i] = &a[i];
    57     }
    58     int **pp;
    59     pp = p;//把指针数组p的首地址给pp
    60     for (int i = 0; i < 5; i++){
    61         cout << **pp << " ";
    62         pp++;
    63     }
    64 }
    65 void main(){
    66     pointer function;
    67     function._cinvlauemn();
    68     function._cinvaluea();
    69     function._swappointer();
    70     function._pointerarray();
    71 }

    4、运行结果

  • 相关阅读:
    推荐文章:深入浅出REST
    推荐Fowler作序的新书《xUnit Test Patterns》
    测试替身:Test Double
    踢毽也能治胃病,适当的运动带来健康,健康带来快乐
    10分钟入门AOP:用PostSharp普及一下AOP
    推荐一本新的英文版算法书和一本讲debug的书
    emacs开发rails的演示
    转:一个土木工程师在四川地震灾后的思考
    多语言多范型编程PPP
    巧用editplus学习正则表达式
  • 原文地址:https://www.cnblogs.com/hehesunshine/p/11609530.html
Copyright © 2011-2022 走看看