Nevin Chen
Nevin Chen

Reputation: 1815

Using typedef enum as a new type

I got a beginner question and I've surfed through the Internet and only find definition like

typedef enum
{
    A,B,C,D
}CAP;
CAP a=A; // printf("%d",a); => 1

But my question is (from Stanford CS107 section handout ) that :

typedef enum { 
 Integer, String, List, Nil 
} nodeType; 
// skip
char *ConcatAll(nodeType *list) 
{ 
 switch (*list) { 
 case Integer: 
 case Nil: return strdup(""); 
 case String: return strdup((char *)(list + 1)); 
 } 
 nodeType **lists = (nodeType **)(list + 1); 
 // skip after
} 

Since the nodeType is numeric (1 , 2, 3), how come it could be used as type declaration

nodeType *list;

and even this?

nodeType **lists = (nodeType **)(list + 1); 

Or maybe there's a manual so I can find? Thank you for your kind advice!

Upvotes: 0

Views: 174

Answers (1)

Barmar
Barmar

Reputation: 780673

When you define a type with typedef, you can use it wherever a type can be used. It's treated as if you'd used the type that was defined. So:

nodeType *list;

is equivalent to:

enum {Integer, String, List, Nil} *list;

Upvotes: 1

Related Questions