指针与地址

2018-05-11  本文已影响0人  愤怒小鸟飞呀飞

Person *p = [[Person alloc] init];
NSLog(@"p对象的地址:%p",p);
NSLog(@"p指针的地址:%p",&p);
优秀博客地址:
https://www.cnblogs.com/lulipro/p/7460206.html

 //打印对象的内存地址
       NSLog(@"内存地址1:%p",a);
       //打印指针自己的内存地址
       NSLog(@"内存地址2:%p",&a); 
  id classObject = objc_getClass([@"Person" UTF8String]);
    
    unsigned int count = 0;
    Ivar *ivarList = class_copyIvarList(classObject, &count);
    //赋值
//    NSLog(@"ivar:%s",Ivar);
    for (int i = 0; i < count; i++) {
        Ivar ivar = ivarList[i];
        NSString *memberName = [NSString stringWithUTF8String:ivar_getName(ivar)];
        NSLog(@"============ivarName:%@",memberName);
      
        Person *p = [[Person alloc] init];
        object_setIvar(p, ivar, @"苹果");
        NSLog(@"============p.name:%@",p.name);
    }

如上述代码所示,Ivar *ivarList ,ivar数组指针,
Ivar ivar = ivarList[i]; 地址变量

class_copyIvarList实现如下

Ivar *class_copyIvarList(Class cls, unsigned int *outCount)
{
    Ivar *result = nil;
    unsigned int count = 0;
    int i;

    if (!cls) {
        if (outCount) *outCount = 0;
        return nil;
    }

    if (cls->ivars) {
        count = cls->ivars->ivar_count;
    }

    if (count > 0) {
        result = (Ivar *)malloc((count+1) * sizeof(Ivar));

        for (i = 0; i < cls->ivars->ivar_count; i++) {
            result[i] = (Ivar)&cls->ivars->ivar_list[i];
        }
        result[i] = nil;
    }

    if (outCount) *outCount = count;
    return result;
}

result[i] = (Ivar)&cls->ivars->ivar_list[i];
copylist中返回的数组中存地址
Ivar *result = nil;
返回的数组是一个指针变量

上一篇 下一篇

猜你喜欢

热点阅读