1

I'm learning about structures within structures and typedef definitions. I understand normal typedef definitions, but in this example a typedef is used for struct data points

struct CGPoint{
    CGFloat x;
    CGFloat y;
};

typedef struct CGPoint CGPoint;

CGPoint rectPt;

rectPt.x=2;
rectPt.y=3;

I dont understand typedef struct CGPoint CGPoint; Why is CGPoint listed twice?

ipmcc
  • 29,581
  • 5
  • 84
  • 147
Brosef
  • 2,945
  • 6
  • 34
  • 69
  • 1
    This might help answer your question: http://stackoverflow.com/questions/612328/difference-between-struct-and-typedef-struct-in-c This is not something specific to Objective-C but has to do with how the struct keyword works in C. – Brad Peabody Aug 30 '13 at 01:24
  • This one is also relevant: http://stackoverflow.com/questions/252780/why-should-we-typedef-a-struct-so-often-in-c – Brad Peabody Aug 30 '13 at 01:31

2 Answers2

2

if you don't use

typedef struct CGPoint CGPoint;

you can't write

CGPoint rectPt;

but instead you have to write explicitly

struct CGPoint rectPt;

because in C defining a struct doesn't automatically define a typename for that as it happens in C++

pqnet
  • 6,070
  • 1
  • 30
  • 51
1

After typedef you can use CGPoint instead of struct CGPoint.

A simpler way to achieve this is to combine them like this:

typedef struct CGPoint{
    CGFloat x;
    CGFloat y;
} CGPoint;
Yu Hao
  • 119,891
  • 44
  • 235
  • 294