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.
精彩评论