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、运行结果

  • 相关阅读:
    [野狐行网游研究][二期][8.21更新]
    Movidius的深度学习入门
    Linux下深度学习常用工具的安装
    Intel AI Cloud 使用
    【Effective Java读书笔记】创建和销毁对象(一):考虑使用静态工厂方法代替构造器
    策略模式
    Java 8 中常用的函数式接口
    MySQL权限管理(五)
    kickstart无人值守安装
    pymysql模块使用
  • 原文地址:https://www.cnblogs.com/hehesunshine/p/11609530.html
Copyright © 2011-2022 走看看