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
  • 4,921
  • 7
  • 31
  • 58

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,334
  • 2
  • 18
  • 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
  • 86,036
  • 7
  • 110
  • 147