zoukankan      html  css  js  c++  java
  • oc const 关键字 对指针的理解

     /*
         int const *p;  *p是常量, p是变量
         const int *p;  *p是常量, p是变量
         
         int * const p; *p是变量, p是常量
         
         const int * const p; *p是常量, p是常量
         
         int const * const p; *p是常量, p是常量
      */
    
    #import <Foundation/Foundation.h>
    #import <objc/runtime.h>
    
    /**
     const的作用
     1.const只修饰它右边的内容
     2.被const修饰的变量是只读的(变量 -> 只读变量[常量])
     */
    void testAge(const int *p);
    
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            int age = 20;
            
            testAge(&age);
            
            NSLog(@"%d", age);
        }
        return 0;
    }
    
    void testAge(const int *p)
    {
        NSLog(@"%d", *p);
    }
    
    void test()
    {
        // 常量(只读变量)
    //        const int age = 20;
    //        int const age = 20; 
        
        // 定义int类型的变量
        int a = 10;
        int age = 20;
        int money = 100;
        
        // 定义指针变量 : 将来只能指向int类型的变量(只能存储int类型变量的地址)
        int const * const p = &a;
        
        // 指针变量p 指向 age
        //        p = &age;
        //        *p = 90;
        //
        //        // 指针变量p 指向 money
        //        p = &money;
        //        *p = 90;
        
        NSLog(@"%d %d", age, money);
    }
  • 相关阅读:
    03Qt信号与槽(2)
    01Qt中的隐式共享
    10GNU C语言函数调用
    09GNU C语言程序编译
    第一本C语言笔记(下)
    07控制器和控制卡(3)
    06控制器和控制卡(2)
    集合
    linux指令(目录类操作指令)
    面向对象三大特征
  • 原文地址:https://www.cnblogs.com/developer-ios/p/4875650.html
Copyright © 2011-2022 走看看