开发者

Allocate CFString in subfunction

开发者 https://www.devze.com 2023-02-28 19:35 出处:网络
I want to allocate space for CFString in a subfunction. Something like this: void fun(CFStringRef *ref)

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

精彩评论

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