asterisk within typecast operator

814 views Asked by At

The following is an unexplained example in Stephen G. Kochan. “Programming in Objective-C".

id   myNumber;
Fraction *myFraction;
  ...
myFraction = (Fraction *) myNumber;

What is the function of the * within the type cast operator? What would happen if the asterisk was removed from the type cast operator?

myFraction = (Fraction) myNumber;
1

There are 1 answers

0
Wil Shipley On

“Fraction” can be thought of as a structure—lots of bytes packed together. Adding the asterisk says the variable is just a pointer to that structure—e.g., like an 8-byte address instead of the whole structure.

Since structures are awkward to pass around by convention (and later by law) we pass around pointers.

In your particular case, the cast isn’t needed at all—“id” automatically converts to other types, so this is just useless wordiness. Stephen G. Kochan got it wrong.