Structures

Collection of different data types under a single identifier name, terminated with a semi-colon ;

 

Declared as follows:

 

struct identifier {

member1 ;

member2 ;

memberN ;

} ;

 

e.g.

struct bike {

char rider[ ] ;

int place ;
float speed ;
} ;

 

Once declared, the named structure creates a new data type that can be used just like any other data type:

 

data type identifier ; //e.g. int myVar ;

bike ducati ;
bike yamaha, honda ;

 

Alternatively a structure can be declared with identifiers after the closing } and before the ; as follows:

 

struct bike {

char rider[ ] ;

int place ;
float speed ;
} ducati, yamaha, honda ;

 

Individual members can be referred to by using the . dot member operator, which connects the structure name with the member name:

 

ducati.speed

 

Compile & run:

Lorenzo, 254.12, 1
Pedrosa, 267.83, 2
Valentino, 246.87, 6
DePuniet, 0.00, 0
, 0.00, 0

 

 


 

typedef struct

provides a shortcut instead of having to write out the full syntax, struct myStruct { . . . } ;

 

typedef struct identifier {

int memberOne ;

char memberTwo ;

float memberThree ;

} myStruct ;

 

myStruct can now be used instead of having to use the keyword, struct, before each declaration of a new structure.

Leave a Reply