• TestPointer


     C++ Code 
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
     
    /*
        version: 1.0
        author: hellogiser
        blog: http://www.cnblogs.com/hellogiser
        date: 2014/6/3
    */

    #include "stdafx.h"
    #include <iostream>
    using namespace std;

    void fun(int a[][3])
    {
        
    // when a is passed to function,a will become a array pointer
        // like int (*p)[3], so sizeof(a)==4
        cout << sizeof(a) << endl; // 4
    }

    void test_2dim()
    {
        
    // array pointer
        int a[2][3] = {123456};
        
    int (*p)[3];
        p = a;
        fun(a);

        
    // test sizeof
        // for a
        cout << sizeof(a) << endl; // 24
        cout << sizeof(a + 0) << endl; // 4  (24 in debug local watch)
        cout << sizeof(a + 1) << endl; // 4  (24 in debug local watch)

        cout << 
    sizeof(a[0]) << endl; // 12
        cout << sizeof(a[1]) << endl; // 12
        cout << sizeof(*(a + 0)) << endl; // 12
        cout << sizeof(*(a + 1)) << endl; // 12
        cout << sizeof(a[0][0]) << endl; // 4

        
    // for p
        cout << sizeof(p) << endl; // 4
        cout << sizeof(p + 0) << endl; // 4
        cout << sizeof(p + 1) << endl; // 4

        cout << 
    sizeof(p[0]) << endl; // 12
        cout << sizeof(p[1]) << endl; // 12
        cout << sizeof(*(p + 0)) << endl; // 12
        cout << sizeof(*(p + 1)) << endl; // 12
        cout << sizeof(p[0][0]) << endl; // 4


        
    // pointer array
        int *r[2];
        
    int i = 1;
        
    int j = 2;
        r[
    0] = &i;
        r[
    1] = &j;
        cout << 
    sizeof(r) << endl; // 8
    }

    void test_1dim()
    {
        
    int a[2] = {12};
        
    int *p = a;
        
    int *p1 = (int *)&a;
        
    int *p2 = (int *)(&a + 1);

        
    int (*b)[2]; // array pointer
        b = &a;
        
    int *b1 = (int *)b;
        
    int *b2 = (int *)(b + 1);

        cout << 
    "ok" << endl;
    }

    void test_string()
    {
        
    char *str1 = "abcd";
        
    char str2[] = "1234";

        cout << str1 << endl; 
    // abcd
        cout << str2 << endl; // 1234
        cout << *str1 << endl; // a
        cout << *str2 << endl; // 1

        
    //str1[0]='X'; // ERROR
        str2[0] = 'Y'// right
        cout << *str2 << endl; // Y
    }

    void funstr(char str[])
    {
        printf(
    "After transform:%d "sizeof(str));  //4
    }

    void test_array_as_param()
    {
        
    char strs[] = "abcdefg";
        printf(
    "Before transform:%d "sizeof(strs)); //8
        funstr(strs);
    }

    void test_a()
    {
        
    // (int&)a treat a memory as a integer
        float a = 1.0f;
        cout << (
    int)a << endl;  // 1
        cout << (int &)a << endl; // 1065353216
        cout << boolalpha << ( (int)a == (int &)a ) << endl; // false

        
    float b = 0.0f;
        cout << (
    int)b << endl; // 0
        cout << (int &)b << endl; // 0
        cout << boolalpha << ( (int)b == (int &)b ) << endl; // true
    }

    个人学习笔记,欢迎拍砖!---by hellogiser

    Author: hellogiser
    Warning: 本文版权归作者和博客园共有,欢迎转载,但请保留此段声明,且在文章页面明显位置给出原文连接。Thanks!
    Me: 如果觉得本文对你有帮助的话,那么【推荐】给大家吧,希望今后能够为大家带来更好的技术文章!敬请【关注】
  • 相关阅读:
    Linux 安装 iptables防火墙
    CentOS最常用命令及快捷键整理
    WebAPI 和 webservice接口
    Linux 文件权限
    Linux查看系统信息的一些命令及查看已安装软件包的命令
    navicat连接虚拟机(centos)中的mysql
    Nmap扫描与Tcpdump抓包分析
    python 识别验证码自动登陆
    iptables开通某些端口
    hive的安装和使用
  • 原文地址:https://www.cnblogs.com/hellogiser/p/test-pointer.html
Copyright © 2020-2023  润新知