-1

I can't say I've used a lot of typedefs, but in Cocoa Touch, it's is a little confusing. Take for instance CoreGraphics' own definition for CGPoint:

struct CGPoint {
    CGFloat x;
    CGFloat y;
};
typedef struct CGPoint CGPoint;

If I were to define this from what I've seen in books, go:

typedef struct {
    CGFloat x;
    CgFloat y;
} CGPoint; 

and it seems to be working perfectly fine. So is there a difference in what these are doing, or are these doing the exact same thing?

  • 3
    BTW, Xcode doesn't define anything. Xcode is an Integrated Development Environment (IDE). Those `typedef`s are from the MacOS X or iOS headers. You should try to gain an understanding of the difference between the system and the tools you're using. – user1118321 Mar 17 '13 at 15:30
  • Fixed, this really isn't even remotely related to Xcode, **when you people learn that it's not Xcode that defines types, compiles your code and wipes your bottom?** –  Mar 17 '13 at 16:39

2 Answers2

2

Apple's example is the same as doing this.

typedef struct CGPoint {
    CGFloat x;
    CGFloat y;
} CGPoint;

The difference is that in code with Apple's definition, you could define a variable as either struct CGPoint OR CGPoint. In your typedef you have basically created a nameless struct, that you then call a CGPoint, rather than a struct named CGPoint that you also call a CGPoint.

Often you will see typedefs have the 'struct' part replaced with something like CGPoint_t

EDIT: Consider the following.

typedef struct {
    List *next;  // <-- compiler error
} List;

Why? Because the type 'List' isn't known to the compiler yet.

typedef struct List {
    struct List *next; // <-- works because you named your struct, and used that name here
} List;

If you do not name your struct, you cannot include itself (pointer) as a member.

Jared Kipe
  • 1,189
  • 6
  • 5
  • Is there any specific advantage of giving a name to the struct before typedef? – funtime Mar 17 '13 at 15:37
  • 1
    Basically it is a convention. There is only one example of when you NEED to name a struct (that I know of) and that is when the struct depends on itself. I will update my answer to provide code. – Jared Kipe Mar 17 '13 at 15:43
  • Ah, I see, thanks! Glad to see there are people willing to be nice to noobs out there! – user1615285 Mar 17 '13 at 19:28
1

In C language, struct names (called tags) live in their own namespace. struct Foo names a different thing than Foo:

struct Foo {
   int smth;
};

int main() {
    struct Foo foo; // OK. note `struct`, it is required
    // Foo nope; // ERROR, no such type Foo
}

In C++, struct Foo and Foo are the same thing.

If one wants to use just Foo, like in C++ (and like our intuition is telling us!), they have to create a typedef named Foo for the struct Foo type. This is what Apple's definition of CGPoint is doing.

You can combine them both into one statement:

typedef struct Foo {
   int smth;
} Foo;

This is how those books advise you to do it.

hamstergene
  • 24,039
  • 5
  • 57
  • 72