1

Here's some instance variables in a header file from an iphone game develpment book:

CGFloat *point;
int count;
CGFloat vectorScale;

Now I'm absolutaley confused. What's that asterisk before the point ivar? Is CGFLoat a class as well as it's not?

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
Mikayil Abdullayev
  • 12,117
  • 26
  • 122
  • 206

3 Answers3

4

A CGFloat is just a C typedef for float or a double depending on your platform. On modern machines its probably a double. This means that *point as a pointer to a float. Given that name, its probably (educated guess) a pointer to an interleaved array of x y coordinates.

aLevelOfIndirection
  • 3,522
  • 14
  • 18
  • I know that it's a double or float (depending on the CPU) in the core. What's the difference between CGFloat point and CGFloat *point? – Mikayil Abdullayev Sep 08 '11 at 06:23
  • @Mikayil: `CGFloat point` declares a variable named `point` that holds a value of type `CGFloat`. `CGFloat *point` declares a variable that holds *a pointer to* a value of type `CGFloat`. The main reason to do this is that `point` holds a pointer to *at least one* `CGFloat`—i.e., a C array of them. This is all pure C, nothing to do with Objective-C at all. You may find my tutorial on C pointers illuminative: http://boredzo.org/pointers – Peter Hosey Sep 08 '11 at 06:52
2

it's not a class. on iOS, it is a float. on osx, it is float or double.

the asterisk says that point is a pointer to a CGFloat. an introductory C book will explain pointers.

justin
  • 104,054
  • 14
  • 179
  • 226
2

If you want to know what one of these thing is in Xcode, option-click on the type. You will be given the option "Jump to Definition".

In Swift:

public struct CGFloat {
 ...

(contained in Core Graphics)

In your question you were in Objective C:

#if defined(__LP64__) && __LP64__
# define CGFLOAT_TYPE double
# define CGFLOAT_IS_DOUBLE 1
# define CGFLOAT_MIN DBL_MIN
# define CGFLOAT_MAX DBL_MAX
#else /* !defined(__LP64__) || !__LP64__ */
# define CGFLOAT_TYPE float
# define CGFLOAT_IS_DOUBLE 0
# define CGFLOAT_MIN FLT_MIN
# define CGFLOAT_MAX FLT_MAX
#endif /* !defined(__LP64__) || !__LP64__ */

typedef CGFLOAT_TYPE CGFloat;
#define CGFLOAT_DEFINED 1

tl:dr it seems not to have been a class yet but it is a struct, float or double depending where you are.

Adam Eberbach
  • 12,309
  • 6
  • 62
  • 114