开发者

address operator in Objective C

I am porting cpp code to Objective C.

In cpp

void create(INT32u &uLen)
{
 sprintf((char*)ps[uLen],"%x%d",val,val1);
}

In Objective C

-(void)create:(uint32_t**)uLen
{
 sprintf((cha开发者_运维技巧r*)ps[uLen],"%x%d",val,val1);
}

This gives me an error as the index will take only integer value. How do i need to pass the variable. The original variable passed should also change with the changes made with the variable uLen.


Since Objective-C is a strict superset of C you still can do this: pass a pointer to your integer and use the dereference operator (prefix *) to look up the value that exists at that address:

-(void)create:(uint32_t*)uLen
{
    sprintf((char*)ps[(*uLen)],"%x%d",val,val1);
}

However, pass by reference is not very common in Cocoa. Actually, it is largely limited to NSError**. Why not pass a simple integer and return the new value since you are currently returning void?

-(uint32_t)create:(uint32_t)uLen
{
    sprintf((char*)ps[uLen],"%x%d",val,val1);
    ...
    return newValue;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜