al gh
al gh

Reputation: 528

struct dclaration in foundation framework

I have some basic knowledge about objective c and how to declare and use Struct and how to use Typedef in order to call the Struct easier, however I see a lot of weird complicated Struct usage and declaration that I have no idea about. The following is one of them which is defined in "CFUUID.h" class in foundation framework

typedef const struct __CFUUID * CFUUIDRef;

can somebody please explain what is going on here, this is not the usual way to define a Struct according to what I have read so far. So it means this Struct wil be a constant that can not be changed later? Is the name of the Struct _CFUUID? Why do wee need that pointer and what is that typedef doing here in relation to the pointer?

Upvotes: 0

Views: 55

Answers (1)

Amin Negm-Awad
Amin Negm-Awad

Reputation: 16660

I think that SO is no C tutorial. Anyway a short answer.

  1. It is no definition of a struct. It is a definition of a type (typedef).

  2. A type definition is something like an alias: The identifier (CFUUIDRef) stands for the type mentioned ahead of it. (const struct __CFUUID * aka a pointer to a structure with the identifier __CFUUID.).

  3. A type definition has several advantages:

    • It makes code shorter and more readable.

    • It encapsulates information (like classes do). For example the "real" type can be changed without breaking existing code, which only relays on CFUUIDRef instead of its inner definition. (That's why you should not ask, what struct __CFUUID is. They do not want you to ask that. It is a secret.)

    • It makes things easier, if the type is a part of another construction. (I. e. an array.)

  4. Yes, const makes it constant. Nobody should change a UUID.

  5. Using pointers gives another level of indirection. This has many advantages. Additionally passing a pointer around (1 cpu word) is faster than copying the whole struct (2 or 4 cpu words).

Upvotes: 2

Related Questions