• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: CFSTR and double-double quotes
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: CFSTR and double-double quotes


  • Subject: Re: CFSTR and double-double quotes
  • From: "Michael Ash" <email@hidden>
  • Date: Sat, 26 Jul 2008 16:27:22 -0400

On Sat, Jul 26, 2008 at 2:40 PM, Ivan Galic <email@hidden> wrote:
> Hi,
>
> I've been writing a utility function for getting a path within the bundle
> from the filename. For that, I used the function CFBundleCopyResourceURL,
> which needs some CFStringRefs. I saw somewhere on the net an example like
> this:
>
> CFSTR("somefile")
>
> and it worked fine. However, when I put it inside a function which takes
> const char * parameters, it didn't:
>
> void SomeFunction(const char *filename)
> {
> ...
> CFBundleCopyResourceURL(mainBundle, CFSTR(filename), ...);
>
> }
>
> I eventually solved the problem using CFStringCreateFromCString, but there's
> one thing I don't understand. I thought a string literal should be treated
> by the compiler just as const char*, but obviously it isn't. Further, I've
> found in the CoreFoundation headers the definition of CFSTR which uses a
> syntax I've never seen before:
>
> #ifdef __CONSTANT_CFSTRINGS__
> #define CFSTR(cStr)  ((CFStringRef) __builtin___CFStringMakeConstantString
> ("" cStr ""))
> #else
> #define CFSTR(cStr)  __CFStringMakeConstantString("" cStr "")
> #endif
>
>
> Can anyone explain what do the double-double quotes mean? Is it something
> Apple specific? I've never seen it before in a C program...

Look at the expansion when you do, say, CFSTR("somestring"):

__builtin___CFStringMakeConstantString("" "somestring" "")

You have three constant strings in a row (the first and last are just
zero-length strings), and C concatenates adjacent constant strings.
The question is then, why do they concatenate with zero-length
strings, an operation which does nothing? The reason should become
clear when you do, say, CFSTR(somevar):

__builtin___CFStringMakeConstantString("" somevar "")

Here you have a constant string, a variable, and another constant
string. This is a syntax error. The error that it triggers is
intentional. CFSTR is for creating constant, compile-time strings
only, and this "" construction is a simple way to get the compiler to
enforce it.

Mike
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >CFSTR and double-double quotes (From: Ivan Galic <email@hidden>)

  • Prev by Date: Re: How to locate coding examples for specified commands [Newbie]
  • Next by Date: Re: unsigning NSNumber
  • Previous by thread: Re: CFSTR and double-double quotes
  • Next by thread: NSTimer and a problem with document-based apps
  • Index(es):
    • Date
    • Thread