0

I stumbled across the following code when coding. I'm wondering how it even compiles without warning?

NSString *str = 0

Logging it returns (null) so it's not setting the string literally. Why does this work?

Milo
  • 5,041
  • 7
  • 33
  • 59

2 Answers2

2

With this line, you're assigning the zero value to a pointer, which is only a convention to define a null pointer, which doesn't address to any memory.

Here (Why is address zero used for the null pointer?) you have a discussion about why zero is the null pointer convention in c.

Community
  • 1
  • 1
Garoal
  • 2,364
  • 2
  • 19
  • 31
1

In C and, by extension, Objective-C, 0 is the equivalent of NULL and nil and Nil. It's an integer but it freely assigns to any pointer type.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154