• 10_引用、宏定义、typedef


    一 引用:给变量取别名

        int num = 10;
    int& N = num; // 给num取别名叫做N(N是num的引用)

    指针和引用的区别:

    1 指针可以为空 引用不能为空

    2 指针可以不初始化 引用必须初始化

    3 指针可以改变指向 引用不能改变被引用对象

    4 指针有自己的内存 引用与被引用对象共享同一段内存

     

    二 宏定义:完全替换

    #include <iostream>
    using namespace std;

    #define NUM 10
    #define ADD1 2+2
    #define ADD2 (2+2)
    #define ADD(x,y,z) x=x+10;y=y-1;z=x+y

    int main()
    {
    int a = NUM;
    cout << a << endl; // 10

    int b = a * ADD1;
    cout << b << endl;  // 22

    int c = a * ADD2;
    cout << c << endl;  // 40

    // 带参数的宏定义(带参宏)
    ADD(a, b, c);
    cout << c << endl;

    return 0;
    }

    三 typedef:给类型取别名

    // 给int类型取别名为I
    typedef int I;

    // 给int [10] 这个类型取别名 叫做ARR
    typedef int ARR [10];

    // main():
    I a = 0;
    ARR arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };

     

     

     

     

     

     

     

     

     

     

     

  • 相关阅读:
    LeetCode Power of Three
    LeetCode Nim Game
    LeetCode,ugly number
    LeetCode Binary Tree Paths
    LeetCode Word Pattern
    LeetCode Bulls and Cows
    LeeCode Odd Even Linked List
    LeetCode twoSum
    549. Binary Tree Longest Consecutive Sequence II
    113. Path Sum II
  • 原文地址:https://www.cnblogs.com/ZhenXin0101/p/13524871.html
Copyright © 2020-2023  润新知