Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Objective-C Basics (Retired) Pointers and Memory Structs

Sphere makeSphere

does typedef make sphere into a class? is that why he puts Sphere in front of makeSphere?

2 Answers

David Kaneshiro Jr.
David Kaneshiro Jr.
29,247 Points

Hi Anusha Chillara,

The typedef key word does not make Sphere into a class. Sphere is still a struct. The typedef keyword allows you to use the name Sphere as the new data type name for the struct without having to preface it with key word struct every time you use the Sphere data type in a variable or function declaration.

For example:

// Creating a struct without typedef
struct sphere {
   int x;
   int y;
   int radius;

// Since we are not using the keyword typedef whenever we use the struct sphere we have to use
// the whole qualifying name if we want to declare a variable of type struct sphere.

struct sphere shape1;

// And when we want to use the struct sphere as a return type for a function or a parameter in 
// a function we would have to do the same thing within the function's declaration and implementation 
// as well.

struct sphere *someFunctionDeclaration(struct sphere *parameter1);

As you can see having to type the keyword struct every time you want to use the struct sphere data type will get tedious, and makes your code a bit harder to read.

So by using the typedef keyword the code example above will look like this:

typedef struct sphere{
   int x;
   int y;
   int radius;
} Sphere;

Sphere shape1;

Sphere *someFunctionDeclaration(Sphere *sphere);

Check out the following web page: http://www.tutorialspoint.com/cprogramming/c_typedef.htm

It will show you more uses for typedef.

Thanks! This makes it clear!