开发者

Allocate CFString in subfunction

I want to allocate space for CFString in a subfunction. Something like this:

void fun(CFStringRef *ref)
{
    ref = CFStringCreateWithCString(NUL开发者_Go百科L, "hi", kCFStringEncodingUTF8);
}

int main(void)
{
    CFStringRef ref;
    fun(&ref);

    CFShow(ref);

    if(ref) CFRelease(ref);
    return 0;
}

It does compile with a warning and doesn't print "hi". What's wrong here?

EDIT: added CFRelease()


CFStringCreateWithCString() returns a CFStringRef, not a CFStringRef *. You have to dereference the pointer in the assignment:

*ref = CFStringCreateWithCString(NULL, "hi", kCFStringEncodingUTF8);

Edit: for your next question, please don't just say "it compiles with a warning". Tell us the actual warning message. It makes answering your question so much easier.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜