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);
}
原文地址:https://www.cnblogs.com/developer-ios/p/4875650.html