• 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: [Somewhat-OT] Unexpected behaviour of variable argument lists
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Somewhat-OT] Unexpected behaviour of variable argument lists


  • Subject: Re: [Somewhat-OT] Unexpected behaviour of variable argument lists
  • From: Gwynne <email@hidden>
  • Date: Thu, 16 Sep 2004 09:26:02 -0400

On Sep 16, 2004, at 9:03 AM, j o a r wrote:
Consider the following log statement with an incorrect, or at least unusual, set of arguments:

	int someIntegerValue = 3;
	NSLog(@"A format string: %d" "Some other string", someIntegerValue);

I would have expected it to generate a compiler error, but for some reason it doesn't even log a warning. Also, it kind of "works" and outputs:

	A format string: 3Some other string

Apparently there is something I don't know or understand about what happens to arguments passed to a function/method that accept a variable number of arguments... Can someone please point me to the fine documentation?

NSLog(@"A format string: %d" "Some other string", someIntegerValue);

There's no comma between the strings, so they're lexically both part of the first parameter. String constants with only whitespace between them are concatenated by the preprocessor; running it through gcc -E or CodeWarrior's or Xcode's Preprocess command would show:

NSLog(@"A format string: %dSome other string", someIntegerValue);

I wasn't aware this worked on @"" NSString constants, though. If you'd written it like this:

NSLog(@"A format string: %d", "Some other string", someIntegerValue);

The result would have been a pointer to the NSConstantString (or whatever it's called) instance shoved into the executable's __DATA (I think?) section by the compiler (under Xcode/gcc anyway; in CW I have no idea how constant NSStrings are handled).

-- Gwynne, key to the Code that runs us all
Email: email@hidden
Web: http://musicimage.plasticchicken.com/

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >[Somewhat-OT] Unexpected behaviour of variable argument lists (From: j o a r <email@hidden>)

  • Prev by Date: Re: [Somewhat-OT] Unexpected behaviour of variable argument lists
  • Next by Date: Re: build numbers
  • Previous by thread: Re: [Somewhat-OT] Unexpected behaviour of variable argument lists
  • Next by thread: Re: [Somewhat-OT] Unexpected behaviour of variable argument lists
  • Index(es):
    • Date
    • Thread