What is the Role and Placement of Typedef in Struct in C Programming?

  • Thread starter Thread starter transgalactic
  • Start date Start date
  • Tags Tags
    Line
Join the discussion
Registration is free. Start your own thread to ask a follow-up.
1 reply · 2K views
transgalactic
Messages
1,386
Reaction score
0
first i create this structure :

Code:
struct album { 
  char *name; 
  char *artist; 
  int numTracks; 
}

if i want my variables not to be global
i can write this inside main:

struct album CD1, CD2, CD3;

or
"
typedef struct album Album;

... then inside main:

Album CD1, CD2, CD3; "

i can't understand this last part regarding where to write the typedef line

and what is the role of type of typedef?
 
Physics news on Phys.org
typedef allows you to use different identifiers to reference data types.
ie.

Code:
typedef unsigned short USHORT
typedef unsigned long  ULONG
typedef unsigned int    UINT

int main()
{  
    USHORT myShort = 5;
    ULONG   myLong = 10;
    UINT     sum = myShort+myLong;
    return 0;
}

typedefs should be used before the procedure definitions.